如何定制java选项卡窗格';谁的账单?
我试图将选项卡形状更改为我拥有的图像,但它只是添加了一个图像 我想在这张图上写信,但我不知道该怎么办 当您执行我的代码时,屏幕会显示出来 这是我想要的代码屏幕 这是我的核心代码。多谢各位如何定制java选项卡窗格';谁的账单?,java,eclipse,swing,Java,Eclipse,Swing,我试图将选项卡形状更改为我拥有的图像,但它只是添加了一个图像 我想在这张图上写信,但我不知道该怎么办 当您执行我的代码时,屏幕会显示出来 这是我想要的代码屏幕 这是我的核心代码。多谢各位 import java.awt.*; import java.util.ArrayList; import javax.swing.*; import img.imageSetSize; public class tabPane extends JFrame { Container c = get
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import img.imageSetSize;
public class tabPane extends JFrame {
Container c = getContentPane();
public tabPane() {
imageSetSize s = new imageSetSize();
setTitle("Kiosk");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(new BorderLayout());
JPanel panelW = new JPanel();
panelW.setLayout(new GridLayout(1, 5, 5, 5));
panelW.setBackground(Color.WHITE);
ImageIcon normalMenuIcon = new ImageIcon("images/UI/normalMenu.png");
normalMenuIcon = s.setSize(normalMenuIcon, 80, 80);
JTabbedPane menuTab = new JTabbedPane(JTabbedPane.LEFT);
menuTab.addTab("", normalMenuIcon, new ItemPanel1());
menuTab.addTab("", normalMenuIcon, new ItemPanel1());
menuTab.addTab("", normalMenuIcon, new ItemPanel1());
menuTab.addTab("", normalMenuIcon, new ItemPanel1());
menuTab.addTab("", normalMenuIcon, new ItemPanel1());
menuTab.setBackground(null);
panelW.add(menuTab);
c.add(panelW, BorderLayout.WEST);
c.setBackground(Color.WHITE);
setSize(450, 740);
setVisible(true);
setResizable(true);
}
public class imageSetSize {
public ImageIcon setSize(ImageIcon icon, int x, int y) {
Image img = icon.getImage();
Image imgScale = img.getScaledInstance(x, y, java.awt.Image.SCALE_SMOOTH);
ImageIcon imgScaled = new ImageIcon(imgScale);
return imgScaled;
}
}
class ItemPanel1 extends JPanel { //implement later
public ItemPanel1() {
this.setBackground(Color.white);
this.setLayout(new GridLayout(3, 3, 0, 0));
}
}
public static void main(String[] args) {
tabPane tp = new tabPane();
}
}
如果您希望文本位于图像的中心,则需要将图标和文本组合成单个图标 有几种方法可以做到这一点: 1) 创建自定义图标 扩展ImageIcon,使其接受两个参数:参数:
paintIcon(…)
方法中,您的代码可能看起来像:
class TextImageIcon extends ImageIcon
{
String text;
public TextImageIcon(String text, Image image)
{
super(image);
this.text = text;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y); // paint the image
// add text centered on the image
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth( text );
int textX = (getIconWidth() - stringWidth) / 2;
int textY = getIconHeight() / 2;
g.drawString(text, textX, textY)
}
)
JLabel label = new JLabel("your text");
label.setIcon( yourIcon );
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
BufferedImage image = ScreenImage.createImage( label );
ImageIcon iconWithText = new ImageIcon( image );
2) 创建JLabel的映像
JLabel允许您以图标为中心显示文本。因此,您可以让JLabel来完成工作,而不是自己进行自定义绘制。创建标签后,您将创建标签的图像以用作图标
基本代码类似于:
class TextImageIcon extends ImageIcon
{
String text;
public TextImageIcon(String text, Image image)
{
super(image);
this.text = text;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y); // paint the image
// add text centered on the image
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth( text );
int textX = (getIconWidth() - stringWidth) / 2;
int textY = getIconHeight() / 2;
g.drawString(text, textX, textY)
}
)
JLabel label = new JLabel("your text");
label.setIcon( yourIcon );
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
BufferedImage image = ScreenImage.createImage( label );
ImageIcon iconWithText = new ImageIcon( image );
使用该类是创建图像的最简单方法,尽管它确实向您显示了创建标签图像的基本代码。如果您希望文本位于图像的中心,则需要将图标和文本组合成单个图标 有几种方法可以做到这一点: 1) 创建自定义图标 扩展ImageIcon,使其接受两个参数:参数:
paintIcon(…)
方法中,您的代码可能看起来像:
class TextImageIcon extends ImageIcon
{
String text;
public TextImageIcon(String text, Image image)
{
super(image);
this.text = text;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y); // paint the image
// add text centered on the image
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth( text );
int textX = (getIconWidth() - stringWidth) / 2;
int textY = getIconHeight() / 2;
g.drawString(text, textX, textY)
}
)
JLabel label = new JLabel("your text");
label.setIcon( yourIcon );
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
BufferedImage image = ScreenImage.createImage( label );
ImageIcon iconWithText = new ImageIcon( image );
2) 创建JLabel的映像
JLabel允许您以图标为中心显示文本。因此,您可以让JLabel来完成工作,而不是自己进行自定义绘制。创建标签后,您将创建标签的图像以用作图标
基本代码类似于:
class TextImageIcon extends ImageIcon
{
String text;
public TextImageIcon(String text, Image image)
{
super(image);
this.text = text;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
super.paintIcon(c, g, x, y); // paint the image
// add text centered on the image
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth( text );
int textX = (getIconWidth() - stringWidth) / 2;
int textY = getIconHeight() / 2;
g.drawString(text, textX, textY)
}
)
JLabel label = new JLabel("your text");
label.setIcon( yourIcon );
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
BufferedImage image = ScreenImage.createImage( label );
ImageIcon iconWithText = new ImageIcon( image );
使用该类是创建图像的最简单方法,尽管like向您展示了创建标签图像的基本代码。您可以添加带有文本的图像。如果要添加动态文本,则可以添加文本视图并使用边框背景,然后可以向其中添加文本而不是使用图像。您不必使用
JTabbedPane
。您可以有一个JPanel
包含您的按钮,即事件、设置、侧边等,另一个JPanel
包含。单击按钮时,显示其关联的JPanel
。要更改JTabbedPane
中的实际选项卡,可能需要进行自定义,您可以添加带有文本的图像。如果要添加动态文本,则可以添加文本视图并使用边框背景,然后可以向其中添加文本而不是使用图像。您不必使用JTabbedPane
。您可以有一个JPanel
包含您的按钮,即事件、设置、侧边等,另一个JPanel
包含。单击按钮时,显示其关联的JPanel
。要更改JTabbedPane
中的实际选项卡,可能需要进行自定义