單擊按鈕時,我想顯示標籤。我正在使用Eclipse Juno。我有標籤添加和設置可見部分...在Java swing中單擊按鈕後顯示標籤
wLabel = new JLabel("YOu and Me");
wLabel .setVisible(false);
wLabel .setBounds(80, 35, 100, 25);
wLabel .setFont(new Font("Meiryo", Font.PLAIN, 9));
wLabel .setForeground(new Color(255, 102, 21));
add(wLabel);
按鈕
wButton = new JButton("W");
wButton .setActionCommand("myButton");
wButton .addActionListener(this);
wButton .setFont(new Font("Meiryo UI", Font.PLAIN, 11));
wButton .setBounds(10, 33, 70, 35);
wButton .setBackground(new Color(102, 51, 20));
add(wButton);
而這裏的actionPerformed。我已經實現的ActionListener
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("myButton")) {
wLabel.setVisible(true);
}
}
wButton =新的JButton( 「W」); \t \t wButton.setActionCommand(「myButton」); \t \t wButton.setFont(new Font(「Meiryo UI」,Font.PLAIN,11)); \t \t wButton.setBounds(10,32,80,30); \t \t wButton.setBackground(new Color(102,51,0)); \t \t add(wButton); \t \t wButton.addActionListener(新的ActionListener(){ \t \t \t公共無效的actionPerformed(ActionEvent的五){ \t \t \t \t如果(e.getActionCommand()。等於( 「myButton的」)){ \t \t \t \t \t wLabel.setVisible(true); \t \t \t} \t \t}}); – JumboJey 2014-09-19 10:50:31
如果我這樣做, – JumboJey 2014-09-19 10:50:48
嘗試將例如System.out.println()檢查是否調用方法。如果它不起作用,請嘗試刪除'if(..)'語句 – 2014-09-19 10:57:53