我尝试搜索某种互动JLabel
。我希望它看起来像这样:
我不确定这是什么叫,或者我在哪里可以找到有关显示此内容的信息。我希望它在按下+/-时打印刷新的数字。我让它在eclipse控制台上打印,但我不确定如何将它打印到JFrame
。
以下是一些代码:
String input = JOptionPane.showInputDialog("Please enter the number of laps.");
numLaps = Integer.parseInt(input);
//frame creation
JFrame f = new JFrame("Number of Laps");
f.setSize(550, 450);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
// label creation
JLabel label = new JLabel("You entered " + numLaps + " laps. Press + to add a lap. Press - to subtract a lap.", SwingConstants.CENTER);
label.setBounds(0,0,500,300);
f.add(label);
//display window
f.setVisible(true);
//button creation add
JButton add = new JButton ("+");
add.setBounds(350,250,50,50);
add.setPreferredSize(new Dimension(50,50));
add.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// what happens when button is pressed
//numLaps++;
addToLap();
System.out.println(numLaps);
}
});
f.add(add);
//button creation subtract
JButton sub = new JButton ("-");
sub.setBounds(100,250,50,50);
sub.setPreferredSize(new Dimension(50,50));
sub.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// what happens when button is pressed
//numLaps--;
subToLap();
System.out.println(numLaps);
}
});
f.add(sub);
&安培; add / sub方法:
private static void addToLap() {
// TODO Auto-generated method stub
numLaps++;
}
private static void subToLap() {
// TODO Auto-generated method stub
numLaps--;
}
答案 0 :(得分:0)
您可以尝试以下方法:
private int numLaps = 0 // Or the number you want initialize with
然后在你的方法中应该将numLaps值赋予JLabel,如下所示:
this.myLabel.setText(String.valueOf(numLaps));
我希望它有所帮助。