Java小程序-单击按钮时如何显示图像?
嗨,这里是Stack Overflow。我正在尝试制作一个小程序,它将显示所选按钮的图像包,我是java编程新手。有人能帮助我如何使用代码来显示图像吗?这是我的密码:Java小程序-单击按钮时如何显示图像?,java,applet,Java,Applet,嗨,这里是Stack Overflow。我正在尝试制作一个小程序,它将显示所选按钮的图像包,我是java编程新手。有人能帮助我如何使用代码来显示图像吗?这是我的密码: public void init() { // Buttons/Labels/Etc setBackground(Color.white); this.setLayout(null); packageB = new Button("Budget"); add(packageB); p
public void init()
{
// Buttons/Labels/Etc
setBackground(Color.white);
this.setLayout(null);
packageB = new Button("Budget");
add(packageB);
packageB.setBounds(200, 20, 80, 20);
packageP = new Button("Premium");
add(packageP);
packageP.setBounds(300, 20, 80, 20);
sButton = new Button("Silver");
add(sButton);
sButton.setBounds(100, 20, 80, 20);
sButton.setVisible(false);
gButton = new Button("Gold");
add(gButton);
gButton.setBounds(200, 20, 80, 20);
gButton.setVisible(false);
pButton = new Button("Platinum");
add(pButton);
pButton.setBounds(300, 20, 80, 20);
pButton.setVisible(false);
dButton = new Button("Diamon");
add(dButton);
dButton.setBounds(400, 20, 80, 20);
dButton.setVisible(false);
backButton = new Button("<-- Back");
add(backButton);
backButton.setBounds(500, 20, 80, 20);
backButton.setVisible(false);
notice = new Label("CHOOSE YOUR PACKAGE!");
add(notice);
notice.setBounds(215, 0, 200, 20);
notice2 = new Label("WHAT PACKAGE DO YOU WANT?");
add(notice2);
notice2.setBounds(205, 0, 200, 20);
notice2.setVisible(false);
sLabel = new Label("Test Label");
add(sLabel);
sLabel.setBounds(205, 0, 200, 20);
sLabel.setVisible(false);
//adds an ActionListener to all of the buttons to be able to receive commands
packageB.addActionListener(this);
packageP.addActionListener(this);
backButton.addActionListener(this);
sButton.addActionListener(this);
gButton.addActionListener(this);
pButton.addActionListener(this);
dButton.addActionListener(this);
}
public void paint(Graphics g)
{
this.resize(600,400);
g.setColor(Color.black);
g.fillRect(60,50,460,300);
}
// receiver of the ActionListener method
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == packageB)
{
packageB.setVisible(false);
packageP.setVisible(false);
notice.setVisible(false);
notice2.setVisible(true);
backButton.setVisible(true);
}
else if (e.getSource() == packageP)
{
packageB.setVisible(false);
packageP.setVisible(false);
backButton.setVisible(true);
notice.setVisible(false);
notice2.setVisible(true);
sButton.setVisible(true);
gButton.setVisible(true);
pButton.setVisible(true);
dButton.setVisible(true);
}
else if (e.getSource() == backButton)
{
packageB.setVisible(true);
packageP.setVisible(true);
notice2.setVisible(false);
notice.setVisible(true);
backButton.setVisible(false);
sButton.setVisible(false);
gButton.setVisible(false);
pButton.setVisible(false);
dButton.setVisible(false);
}
else if (e.getSource() == sButton)
{
sButton.setBackground(Color.red);
gButton.setBackground(Color.lightGray);
pButton.setBackground(Color.lightGray);
dButton.setBackground(Color.lightGray);
sLabel.setVisible(true);
}
else if (e.getSource() == gButton)
{
sButton.setBackground(Color.lightGray);
gButton.setBackground(Color.red);
pButton.setBackground(Color.lightGray);
dButton.setBackground(Color.lightGray);
}
else if (e.getSource() == pButton)
{
sButton.setBackground(Color.lightGray);
gButton.setBackground(Color.lightGray);
pButton.setBackground(Color.red);
dButton.setBackground(Color.lightGray);
}
else if (e.getSource() == dButton)
{
sButton.setBackground(Color.lightGray);
gButton.setBackground(Color.lightGray);
pButton.setBackground(Color.lightGray);
dButton.setBackground(Color.red);
}
// Add New ActionListener Here!
/*else if (e.getSource() == TestButton)
{
}*/
}
public void init()
{
//按钮/标签/等
挫折地面(颜色:白色);
此.setLayout(null);
packageB=新按钮(“预算”);
添加(包装B);
包装B.立根(200,20,80,20);
packageP=新按钮(“高级”);
添加(packageP);
包装立根(300,20,80,20);
sButton=新按钮(“银色”);
添加(sButton);
b按钮.立根(100,20,80,20);
sButton.setVisible(假);
gButton=新按钮(“金色”);
添加(gButton);
gButton.立根(200,20,80,20);
gButton.setVisible(假);
pButton=新按钮(“白金”);
添加(pButton);
b按钮.立根(300,20,80,20);
pButton.setVisible(假);
dButton=新按钮(“Diamon”);
添加(数据按钮);
b按钮.立根(400,20,80,20);
dButton.setVisible(false);
backButton=新建按钮("避免使用null
layouts,像素完美的布局在现代ui设计中是一种错觉。有太多的因素会影响组件的单个大小,而这些因素都是您无法控制的。Swing的设计核心是与布局管理器配合使用,丢弃这些布局将导致无休止的问题和问题越来越多的时间尝试纠正如果不使用绘制
这种方式,您永远不应该在绘制
方法中修改任何组件的状态,您可以简单地使用设置背景(颜色)<代码> >从<代码> init < /Cord>方法中得到相同的效果,你也会打破画图链,这可能导致任何数量的奇怪的图形化的幻灯片,所以你知道,不是很多人正在积极地使用基于AWT的GUI。你应该考虑查看Swing或JavaFX而不是IMHO1。为什么要编写小程序?如果是老师指定的,请参考。2)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美布局。请使用布局管理器,或者使用布局填充和边框。3)为什么要使用AWT?请参阅o使用组件放弃AWT,转而使用Swing。