使用JButton递增/递减已设置的JTextField时遇到问题。
我知道一个动作侦听器是必需的,但是我不确定如何设置它。我还必须为多个JButton设置它,这会使该过程更加复杂。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class App1 extends JFrame implements ActionListener, MouseListener
{
JPanel upper, lower, jp1, jp2, jp3;
JButton jb1, jb2, jb3, jb4, jb5, jb6;
JLabel jl1, jl2, jl3;
JTextField jtf1, jtf2, jtf3;
int count = 128;
public static void main(String[] args) {
App1 a1 = new App1();
}
public App1() {
JFrame myFrame = new JFrame("Color Selector");
myFrame.setLocationRelativeTo(null);
myFrame.setSize(new Dimension(600,400));
GridLayout layout = new GridLayout(2,1);
myFrame.setLayout(layout);
JPanel upper = new JPanel();
upper.setBackground(Color.GRAY);
JPanel lower = new JPanel(); add(lower);
lower.setLayout(new BoxLayout(lower, BoxLayout.X_AXIS));
lower.setBackground(Color.GRAY);
JPanel jp1 = new JPanel(); lower.add(jp1);
JPanel jp2 = new JPanel(); lower.add(jp2);
JPanel jp3 = new JPanel(); lower.add(jp3);
lower.addMouseListener(this);
JLabel jl1 = new JLabel("Red"); jp1.add(jl1);
JLabel jl2 = new JLabel("Green"); jp2.add(jl2);
JLabel jl3 = new JLabel("Blue"); jp3.add(jl3);
jp1.setBackground(Color.RED);
jp2.setBackground(Color.GREEN);
jp3.setBackground(Color.BLUE);
JButton jb1 = new JButton("R-"); jp1.add(jb1);
JTextField jtf1 = new JTextField(6);jp1.add(jtf1);
jtf1.setText("128");
JButton jb2 = new JButton("R+"); jp1.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
JButton jb3 = new JButton("G-"); jp2.add(jb3);
JTextField jtf2 = new JTextField(6);jp2.add(jtf2);
jtf2.setText("128");
JButton jb4 = new JButton("G+"); jp2.add(jb4);
jb3.addActionListener(this);
jb4.addActionListener(this);
JButton jb5 = new JButton("B-"); jp3.add(jb5);
JTextField jtf3 = new JTextField(6);jp3.add(jtf3);
jtf3.setText("128");
JButton jb6 = new JButton("B+"); jp3.add(jb6);
jb5.addActionListener(this);
jb6.addActionListener(this);
myFrame.add(upper);
myFrame.add(lower);
myFrame.setVisible(true);
}
private void start() {
// TODO Auto-generated method stub
}
private static String setEditable(String string) {
// TODO Auto-generated method stub
return null;
}
private static String setText(String string) {
// TODO Auto-generated method stub
return null;
}
private static void add(JPanel lower) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
private void rebuild() {
// TODO Auto-generated method stub
}
}
我希望JButton使用JButton可以通过+1或-1将JTextField“ 128”设置为增/减。
答案 0 :(得分:1)
在您的actionPerfomed()
方法中,您可以确定用e.getActionCommand()
单击了哪个按钮,然后在每种情况下都需要做的事情
@Override
public void actionPerformed(ActionEvent e) {
String sourceButton = e.getActionCommand();
if (sourceButton.equals("B-")) {
// code for B- button
}
else if (sourceButton.equals("B+")) {
// code for B+ button
}
// etc
// etc
}
答案 1 :(得分:1)
在这种情况下,为每个按钮使用单独的动作监听器会更简单。例如:
代替
jb1.addActionListener(this);
jb2.addActionListener(this);
做
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())-1));
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())+1));
}
});
(因此App1
不需要实现ActionListener
)
注意:使用lambda表达式可使上面的代码看起来像:
jb1.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())-1)));
jb2.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())+1)));
侧面说明:您两次声明了变量。
例如,您有一个字段JTextField jtf1
,之后是JTextField jtf1 = new JTextField(6);
。该字段保持为空。