除了按钮,一切都正常。图形用户界面Java
好的,我可以得到文本字段,普通文本,甚至图像来显示,但我不能得到一个按钮来显示。我不确定我做错了什么,因为我已经为其他人做了同样的步骤。任何帮助都很好,谢谢除了按钮,一切都正常。图形用户界面Java,java,swing,button,layout,Java,Swing,Button,Layout,好的,我可以得到文本字段,普通文本,甚至图像来显示,但我不能得到一个按钮来显示。我不确定我做错了什么,因为我已经为其他人做了同样的步骤。任何帮助都很好,谢谢 package EventHandling2; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swi
package EventHandling2;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import EventHandling.GUITest;
public class EventMain extends JFrame{
private JLabel label;
private JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
//gui.setSize(600, 300);
gui.setVisible(true);
gui.setTitle("Button Test");
}
public void EventMain(){
setLayout(new FlowLayout());
button = new JButton ("click for text");
add(button);
label = new JLabel ("");
add(label);
Events e = new Events();
button.addActionListener(e);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
actionListener(e)包含一个小的控制结构错误:
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
改为:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
label.setText("Now you can see words");
}
}
actionListener(e)包含一个小的控制结构错误:
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
改为:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
label.setText("Now you can see words");
}
}
问题在于方法:
void EventMain()
构造函数没有返回类型。只需删除“void”。代码将很好地工作 问题在于方法:
void EventMain()
构造函数没有返回类型。只需删除“void”。代码将很好地工作 首先,您必须删除
EventMain
的构造函数中的void
关键字。然后,创建JPanel
并向其中添加组件,然后将JPanel
添加到JFrame.contentPane
中
以下代码应该可以工作:
public class EventMain extends JFrame {
private final JLabel label;
private final JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x
// close program
gui.setSize(600, 300);
gui.setTitle("Button Test");
gui.setVisible(true);
}
public EventMain() {
// setLayout(new FlowLayout());
JPanel panel = new JPanel(new FlowLayout());
button = new JButton("click for text");
panel.add(button);
label = new JLabel("");
panel.add(label);
Events e = new Events();
button.addActionListener(e);
this.getContentPane().add(panel);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
首先,您必须删除
EventMain
的构造函数中的void
关键字。然后,创建JPanel
并向其中添加组件,然后将JPanel
添加到JFrame.contentPane
中
以下代码应该可以工作:
public class EventMain extends JFrame {
private final JLabel label;
private final JButton button;
public static void main(String[] args) {
EventMain gui = new EventMain();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x
// close program
gui.setSize(600, 300);
gui.setTitle("Button Test");
gui.setVisible(true);
}
public EventMain() {
// setLayout(new FlowLayout());
JPanel panel = new JPanel(new FlowLayout());
button = new JButton("click for text");
panel.add(button);
label = new JLabel("");
panel.add(label);
Events e = new Events();
button.addActionListener(e);
this.getContentPane().add(panel);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("Now you can see words");
}
}
}
不要设置顶级容器的大小。而是布局内容&调用
pack()
(就在setVisible(true)
之前)。若要更快地获得更好的帮助,请发布一个。我已经尝试过这样做,但按钮仍然没有显示“我已经尝试过这样做”。若要更快地获得更好的帮助,请发布一个。@AndrewThompson发生了什么事?除了未使用的import EventHandling.GUITest之外,它简短、自包含、正确且可编译代码>@johnchen902.。正确且可编译,除了..“不,没有例外..”。。它要么是SSCCE,要么不是。不要设置顶级容器的大小。而是布局内容&调用pack()
(就在setVisible(true)
之前)。若要更快地获得更好的帮助,请发布一个。我已经尝试过这样做,但按钮仍然没有显示“我已经尝试过这样做”。若要更快地获得更好的帮助,请发布一个。@AndrewThompson发生了什么事?除了未使用的import EventHandling.GUITest之外,它简短、自包含、正确且可编译代码>@johnchen902.。正确且可编译,除了..“不,没有例外..”。。它要么是SSCCE要么不是。我这样做了,但我仍然看不到任何按钮。谢谢你的帮忙。我做了,但我还是没看到纽扣。谢谢你的接球。是的,就是这样!谢谢。@LukeWorthing或致电gui.EventMain()
Yep就是这样!谢谢。@LukeWorthing或致电gui.EventMain()