我想创建一个彩票计划。
应该有35个数字可供选择,你只能选7个。因此,我创造了35个JButtons。当您单击JButton时,它将使用Action Command获取它的值,并使用JLabel输出该值。
按钮编号1的值为1,按钮编号2的值为2,依此类推。
这是问题所在:当我点击我的按钮时,它会获取它的值(例如12),然后它将JLabel设置为显示“12”。但是当我在此之后单击另一个按钮时,它会更改第一个值而不是显示一个新数字。
我认为它类似于有人想要使用扫描仪而你使用NextInt();我基本上希望能够在第一个之后选择另一个数字。
帮助将不胜感激,因为我基本上已经尝试了一切。
提前致谢。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Firstclass implements ActionListener {
final static String BUTTONPANEL = "Play the lottery";
final static String TEXTPANEL = "Discover nuclear physics";
final static String BUTTONPANEL2 = "Zomg free food";
final static int extraWindowWidth = 200;
final static int extraWindowHeight = 500;
//Lotto buttons
JButton b1,b2,b3,b4,b5,
b6,b7,b8,b9,b10,b11,b12,
b13,b14,b15,b16,b17,b18,
b19,b20,b21,b22,b23,b24,
b25,
b26,b27,b28,b29,b30,b31,b32
,b33,b34,b35;
//Labels
JLabel lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,info;
public void addComponentToPane(Container pane) {
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
//First panel
JPanel card1 = new JPanel() {
//Make the panel wider than it really needs, so
//the window's wide enough for the tabs to stay
//in one row.
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += extraWindowWidth;
size.height += extraWindowHeight;
return size;
}
};
//Setting THESE particular JButtons to the first actionlistener
ActionListener FirstListener = new FirstListener();
//Adding and configuring the JButtons
b1 = new JButton(Integer.toString(1));
b1.addActionListener(FirstListener);
b1.setPreferredSize(new Dimension(50,50));
card1.add(b1);
b2 = new JButton(Integer.toString(2));
b2.addActionListener(FirstListener);
b2.setPreferredSize(new Dimension(50,50));
card1.add(b2);
b3 = new JButton(Integer.toString(3));
b3.addActionListener(FirstListener);
b3.setPreferredSize(new Dimension(50,50));
card1.add(b3);
b4 = new JButton(Integer.toString(4));
b4.addActionListener(FirstListener);
b4.setPreferredSize(new Dimension(50,50));
card1.add(b4);
b5 = new JButton(Integer.toString(5));
b5.addActionListener(FirstListener);
b5.setPreferredSize(new Dimension(50,50));
card1.add(b5);
b6 = new JButton(Integer.toString(6));
b6.addActionListener(FirstListener);
b6.setPreferredSize(new Dimension(50,50));
card1.add(b6);
b7 = new JButton(Integer.toString(7));
b7.addActionListener(FirstListener);
b7.setPreferredSize(new Dimension(50,50));
card1.add(b7);
b8 = new JButton(Integer.toString(8));
b8.addActionListener(FirstListener);
b8.setPreferredSize(new Dimension(50,50));
card1.add(b8);
b9 = new JButton(Integer.toString(9));
b9.addActionListener(FirstListener);
b9.setPreferredSize(new Dimension(50,50));
card1.add(b9);
b10 = new JButton(Integer.toString(10));
b10.addActionListener(FirstListener);
b10.setPreferredSize(new Dimension(50,50));
card1.add(b10);
b11 = new JButton(Integer.toString(11));
b11.addActionListener(FirstListener);
b11.setPreferredSize(new Dimension(50,50));
card1.add(b11);
b12 = new JButton(Integer.toString(12));
b12.addActionListener(FirstListener);
b12.setPreferredSize(new Dimension(50,50));
card1.add(b12);
b13 = new JButton(Integer.toString(13));
b13.addActionListener(FirstListener);
b13.setPreferredSize(new Dimension(50,50));
card1.add(b13);
b14 = new JButton(Integer.toString(14));
b14.addActionListener(FirstListener);
b14.setPreferredSize(new Dimension(50,50));
card1.add(b14);
b15 = new JButton(Integer.toString(15));
b15.addActionListener(FirstListener);
b15.setPreferredSize(new Dimension(50,50));
card1.add(b15);
b16 = new JButton(Integer.toString(16));
b16.addActionListener(FirstListener);
b16.setPreferredSize(new Dimension(50,50));
card1.add(b16);
b17 = new JButton(Integer.toString(17));
b17.addActionListener(FirstListener);
b17.setPreferredSize(new Dimension(50,50));
card1.add(b17);
b18 = new JButton(Integer.toString(18));
b18.addActionListener(FirstListener);
b18.setPreferredSize(new Dimension(50,50));
card1.add(b18);
b19 = new JButton(Integer.toString(19));
b19.addActionListener(FirstListener);
b19.setPreferredSize(new Dimension(50,50));
card1.add(b19);
b20 = new JButton(Integer.toString(20));
b20.addActionListener(FirstListener);
b20.setPreferredSize(new Dimension(50,50));
card1.add(b20);
b21 = new JButton(Integer.toString(21));
b21.addActionListener(FirstListener);
b21.setPreferredSize(new Dimension(50,50));
card1.add(b21);
b22 = new JButton(Integer.toString(22));
b22.addActionListener(FirstListener);
b22.setPreferredSize(new Dimension(50,50));
card1.add(b22);
b23 = new JButton(Integer.toString(23));
b23.addActionListener(FirstListener);
b23.setPreferredSize(new Dimension(50,50));
card1.add(b23);
b24 = new JButton(Integer.toString(24));
b24.addActionListener(FirstListener);
b24.setPreferredSize(new Dimension(50,50));
card1.add(b24);
b25 = new JButton(Integer.toString(25));
b25.addActionListener(FirstListener);
b25.setPreferredSize(new Dimension(50,50));
card1.add(b25);
b26 = new JButton(Integer.toString(26));
b26.addActionListener(FirstListener);
b26.setPreferredSize(new Dimension(50,50));
card1.add(b26);
b27 = new JButton(Integer.toString(27));
b27.addActionListener(FirstListener);
b27.setPreferredSize(new Dimension(50,50));
card1.add(b27);
b28 = new JButton(Integer.toString(28));
b28.addActionListener(FirstListener);
b28.setPreferredSize(new Dimension(50,50));
card1.add(b28);
b29 = new JButton(Integer.toString(29));
b29.addActionListener(FirstListener);
b29.setPreferredSize(new Dimension(50,50));
card1.add(b29);
b30 = new JButton(Integer.toString(30));
b30.addActionListener(FirstListener);
b30.setPreferredSize(new Dimension(50,50));
card1.add(b30);
b31 = new JButton(Integer.toString(31));
b31.addActionListener(FirstListener);
b31.setPreferredSize(new Dimension(50,50));
card1.add(b31);
b32 = new JButton(Integer.toString(32));
b32.addActionListener(FirstListener);
b32.setPreferredSize(new Dimension(50,50));
card1.add(b32);
b33 = new JButton(Integer.toString(33));
b33.addActionListener(FirstListener);
b33.setPreferredSize(new Dimension(50,50));
card1.add(b33);
b34 = new JButton(Integer.toString(34));
b34.addActionListener(FirstListener);
b34.setPreferredSize(new Dimension(50,50));
card1.add(b34);
b35 = new JButton(Integer.toString(35));
b35.addActionListener(FirstListener);
b35.setPreferredSize(new Dimension(50,50));
card1.add(b35);
info = new JLabel("Your numbers are: ");
card1.add(info);
lbl1 = new JLabel("");
card1.add(lbl1);
lbl2 = new JLabel("");
card1.add(lbl2);
lbl3 = new JLabel("");
card1.add(lbl3);
lbl4 = new JLabel("");
card1.add(lbl4);
lbl5 = new JLabel("");
card1.add(lbl5);
lbl6 = new JLabel("");
card1.add(lbl6);
lbl7 = new JLabel("");
card1.add(lbl7);
//Second panel
JPanel card2 = new JPanel();
card2.add(new JTextField("TextField", 20));
//Third panel
JPanel card3 = new JPanel();
card3.add(new JButton("Hardy harr"));
tabbedPane.addTab(BUTTONPANEL, card1);
tabbedPane.addTab(TEXTPANEL, card2);
tabbedPane.addTab(BUTTONPANEL2, card3);
pane.add(tabbedPane, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Slutuppgift");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Firstclass demo = new Firstclass();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
//frame.pack();
frame.setVisible(true);
frame.setSize(500,700);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private class FirstListener implements ActionListener {
/** This method will be invoked when a button is clicked */
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1 || e.getSource()==b2 || e.getSource()==b3||
e.getSource()==b4 || e.getSource()==b5 || e.getSource()==b6 ||
e.getSource()==b7 || e.getSource()==b8 ||
e.getSource()==b9 || e.getSource()==b10 ||
e.getSource()==b11 || e.getSource()==b12 || e.getSource()==b13 ||
e.getSource()==b14 || e.getSource()==b15 || e.getSource()==b16 ||
e.getSource()==b17 || e.getSource()==b18 || e.getSource()==b19 ||
e.getSource()==b20 || e.getSource()==b21 || e.getSource()==b22 ||
e.getSource()==b23 || e.getSource()==b24 || e.getSource()==b25 ||
e.getSource()==b26 || e.getSource()==b27 || e.getSource()==b28 ||
e.getSource()==b29 || e.getSource()==b30 || e.getSource()==b31 ||
e.getSource()==b32 || e.getSource()==b33 || e.getSource()==b34 ||
e.getSource()==b35) {lbl1.setText(e.getActionCommand());}
}
}
private class SecondListener implements ActionListener {
public void actionPerformed(ActionEvent d) {
if (d.getSource() == b1 || d.getSource()==b2 || d.getSource()==b3||
d.getSource()==b4 || d.getSource()==b5 || d.getSource()==b6 ||
d.getSource()==b7 || d.getSource()==b8 ||
d.getSource()==b9 || d.getSource()==b10 ||
d.getSource()==b11 || d.getSource()==b12 || d.getSource()==b13 ||
d.getSource()==b14 || d.getSource()==b15 || d.getSource()==b16 ||
d.getSource()==b17 || d.getSource()==b18 || d.getSource()==b19 ||
d.getSource()==b20 || d.getSource()==b21 || d.getSource()==b22 ||
d.getSource()==b23 || d.getSource()==b24 || d.getSource()==b25 ||
d.getSource()==b26 || d.getSource()==b27 || d.getSource()==b28 ||
d.getSource()==b29 || d.getSource()==b30 || d.getSource()==b31 ||
d.getSource()==b32 || d.getSource()==b33 || d.getSource()==b34 ||
d.getSource()==b35) {lbl2.setText(d.getActionCommand());}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}`
答案 0 :(得分:0)
您的听众需要知道要更改的标签。学习如何使用数组,然后你的代码就像(伪代码)
int labelToChange = 0;
JLabel labels[];
listener()
{
// This is actually bad: using the GUI as the "source" of data
// but in this case there are so many other issues this is the least
// of your problems...
//
if (labelToChange < 7) {
labels[labelToChange++] = e.getSource().getText();
} else {
// ERROR - too many buttons pressed
}
}
}