在我的计划中,它有两个JButton
,其名称为Start
和Reset to 0
,一个JTextField
和一个JLabel
。当我点击Start JButton
次时,它会在JTextField
显示结果。
如果计数数字等于1,则显示消息Start in JLabel
,当计数大于1时,显示Reset to 0 in JLabel
。但问题是当我点击Reset to 0 JButton
时它重置为1但是没有显示消息Reset to 0 in JLabel
?
计划在这里:
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import com.alee.laf.rootpane.WebFrame;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
public class Search {
private WebFrame frame;
private JTextField textField;
private JButton btnResetTo;
private JButton btnNewButton;
private int count;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Search window = new Search();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Search() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new WebFrame();
frame.setBounds(100, 100, 296, 374);
frame.setDefaultCloseOperation(WebFrame.EXIT_ON_CLOSE);
btnNewButton = new JButton("Start");
btnNewButton.addMouseListener(new MouseAdapter() {
private int Qnty;
@Override
public void mouseClicked(MouseEvent e) {
count=e.getClickCount();
textField.setText(Integer.toString(count));
Qnty=Qnty+count;
if(Qnty==1)
lblNewLabel.setText("Start");
else if(Qnty>1)
{
lblNewLabel.setText("Reset");
}
}
});
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 40));
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setColumns(10);
btnResetTo = new JButton("Reset to 0");
btnResetTo.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
count=e.getClickCount();
count=0;
}
});
lblNewLabel = new JLabel("");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(lblNewLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
.addComponent(btnResetTo, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
.addComponent(textField, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(25)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addComponent(btnResetTo, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addGap(31)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
.addGap(73))
);
frame.getContentPane().setLayout(groupLayout);
}
}
答案 0 :(得分:0)
ActionListener
代替MouseListener
- 可以通过多种方式触发按钮,然后只需点击它们即可。有关详细信息,请参阅std::pair
和How to Use Buttons, Check Boxes, and Radio Buttons btnNew
一样 - 变量没有神奇地链接到组件也许喜欢
btnResetTo = new JButton("Reset to 0");
btnResetTo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
count = 0;
textField.setText(Integer.toString(count));
lblNewLabel.setText("Start");
}
});