我已经写了下面的代码,它似乎工作顺利,除了信息框中的“退出信息”按钮和信息框中的文本。首先,信息框中的文本(这在标签上)拒绝出现。其次,“退出信息”按钮不会出现在我设置的位置。有什么建议我可以做些什么来使其正常运行? 感谢如何让GUI按钮正确响应?
注:我非常缺乏经验与Java,因此可能不会完全理解的答案,除非它们被写入,虽然他们是幼稚园的学生:)
import java.awt.*;
import javax.swing.*;
public class CombineInputWindow {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create the frame
JFrame frame = new JFrame("New GUI");// Title the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// add close function
// Create the label:
JLabel textLabel = new JLabel("");// No text is added
textLabel.setPreferredSize(new Dimension(320,240));// Set the size of the label
frame.getContentPane().add(textLabel, BorderLayout.CENTER);// Add the label to the frame
// The following three lines set the frame up further
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
// Create the panel
JPanel panel = new JPanel();
frame.add(panel);// Add panel to frame
// Create lbutton (size, text, etc.)
JButton lbutton = new JButton("Move Left");
panel.add(lbutton);
lbutton.setSize(128,32);
lbutton.setVisible(true);
lbutton.setLocation(0,208);
// Create rbutton (size, text, etc.)
JButton rbutton = new JButton("Move right");
panel.add(rbutton);
rbutton.setSize(128,32);
rbutton.setVisible(true);
rbutton.setLocation(192,208);
// Create dodge button (size, text, etc.)
JButton btndodge = new JButton("Duck");
panel.add(btndodge);
btndodge.setSize(64,32);
btndodge.setVisible(true);
btndodge.setLocation(128,208);
// Create exit button (size, text, etc.)
JButton btnexit = new JButton("Exit");
panel.add(btnexit);
btnexit.setSize(64,32);
btnexit.setVisible(true);
btnexit.setLocation(256,0);
// Create info button (size, text, etc.)
JButton btninfo = new JButton("Info");
panel.add(btninfo);
btninfo.setSize(64,32);
btninfo.setVisible(true);
btninfo.setLocation(192,0);
// Add function to the lbutton for mouse event
lbutton.addMouseListener(new java.awt.event.MouseAdapter() {
int vbtnclicks = 0;
public void mouseClicked(java.awt.event.MouseEvent evt) {
vbtnclicks = vbtnclicks + 1;
//The following code prints out the number of times the user has clicked the button
if (vbtnclicks > 2) {
System.out.println("You moved left " + vbtnclicks + " times!");
}
else if (vbtnclicks == 1) {
System.out.println("You moved left once!");
}
else if (vbtnclicks == 2){
System.out.println("You moved left twice!");
}
else {
// The following code should not have to show up
System.out.println("I get the sense this code has been meddled with...");
}
}
});
// Add mouse event for rbutton
rbutton.addMouseListener(new java.awt.event.MouseAdapter() {
int cbtnclicks = 0;
public void mouseClicked(java.awt.event.MouseEvent evt) {
cbtnclicks = cbtnclicks + 1;
//The following code prints out the number of times the user has clicked the button
if (cbtnclicks > 2) {
System.out.println("You moved right " + cbtnclicks + " times!");
}
else if (cbtnclicks == 1) {
System.out.println("You moved right once!");
}
else if (cbtnclicks == 2){
System.out.println("You moved right twice!");
}
else {
// The following code should not have to show up
System.out.println("I get the sense this code has been meddled with...");
}
}
});
// Add mouse event for btndodge
btndodge.addMouseListener(new java.awt.event.MouseAdapter() {
int dbtnclicks = 0;
public void mouseClicked(java.awt.event.MouseEvent evt) {
dbtnclicks = dbtnclicks + 1;
//The following code prints out the number of times the user has clicked the button
if (dbtnclicks > 2) {
System.out.println("You ducked " + dbtnclicks + " times!");
}
else if (dbtnclicks == 1) {
System.out.println("You ducked once!");
}
else if (dbtnclicks == 2){
System.out.println("You ducked twice!");
}
else {
// The following code should not have to show up
System.out.println("I get the sense this code has been meddled with...");
}
}
});
// Add mouse event and exit command to the exit button
btnexit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.exit(0);
}
});
// Add function to the "Info" button
btninfo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
// Open an Info Window with specific settings
JFrame inframe = new JFrame("Info");
JLabel inlabel = new JLabel("This is the info text.");
inlabel.setPreferredSize(new Dimension(300,100));
inframe.getContentPane().add(inlabel, BorderLayout.CENTER);
inframe.setLocationRelativeTo(null);
inframe.pack();
inframe.setVisible(true);
JPanel inpanel = new JPanel();
inframe.add(inpanel);
JButton inextbtn = new JButton("Exit Info");
inpanel.add(inextbtn);
inextbtn.setSize(96,24);
inextbtn.setVisible(true);
inextbtn.setLocation(0,0);
inextbtn.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
inframe.dispose();
}
});
}
});
}
}
你不想使用鼠标监听Jbutton将尝试使用事件侦听器来代替。 –
@EliSadoff:MouseListener *是一个事件监听器。也许你想说“ActionListener”,如果是的话,你会是对的。 –
@DontKnowMuchButGettingBetter我的意思是'ActionListener'不是事件监听器。对不起,谢谢! –