I'm stuck again trying to figure out how to out the button clicked to label1.
When I click button01 I get A printed out. I need it to print to the label1 though and say Folder A when A is pressed and B when B is pressed and so on. Any nudge in the right direction?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JFileCabinet extends JFrame {
private JButton button01 = new JButton("A");
private JButton button02 = new JButton("B");
private JButton button03 = new JButton("C");
private JButton button04 = new JButton("D");
private JButton button05 = new JButton("E");
private JButton button06 = new JButton("F");
private JButton button07 = new JButton("G");
private JButton button08 = new JButton("H");
private JButton button09 = new JButton("I");
private JButton button10 = new JButton("J");
private JButton button11 = new JButton("K");
private JButton button12 = new JButton("L");
private JButton button13 = new JButton("M");
private JButton button14 = new JButton("N");
private JButton button15 = new JButton("O");
private JButton button16 = new JButton("P");
private JButton button17 = new JButton("Q");
private JButton button18 = new JButton("R");
private JButton button19 = new JButton("S");
private JButton button20 = new JButton("T");
private JButton button21 = new JButton("U");
private JButton button22 = new JButton("V");
private JButton button23 = new JButton("W");
private JButton button24 = new JButton("X");
private JButton button25 = new JButton("Y");
private JButton button26 = new JButton("Z");
private JButton button27 = new JButton(" ");
private JButton button28 = new JButton(" ");
private JButton button29 = new JButton(" ");
private JButton button30 = new JButton(" ");
private static JLabel label1 = new JLabel("Folder ");
private JPanel panel01 = new JPanel(new GridLayout(1, 6));
private JPanel panel02 = new JPanel(new GridLayout(1, 6));
private JPanel panel03 = new JPanel(new GridLayout(1, 6));
private JPanel panel04 = new JPanel(new GridLayout(1, 6));
private JPanel panel05 = new JPanel(new GridLayout(1, 2));
private GridLayout layout = new GridLayout(5, 1, 5, 5);
public JFileCabinet() {
setLayout(layout);
add(panel01);
add(panel02);
add(panel03);
add(panel04);
add(panel05);
button01.addActionListener(new ButtonListener());
panel01.add(button01);
panel01.add(button02);
panel01.add(button03);
panel01.add(button04);
panel01.add(button05);
panel01.add(button06);
panel02.add(button07);
panel02.add(button08);
panel02.add(button09);
panel02.add(button10);
panel02.add(button11);
panel02.add(button12);
panel03.add(button13);
panel03.add(button14);
panel03.add(button15);
panel03.add(button16);
panel03.add(button17);
panel03.add(button18);
panel04.add(button19);
panel04.add(button20);
panel04.add(button21);
panel04.add(button22);
panel04.add(button23);
panel04.add(button24);
panel05.add(button25);
panel05.add(button26);
panel05.add(button27).setVisible(false);
panel05.add(button28).setVisible(false);
//panel05.add(button29).setVisible(false);
panel05.add(label1);
panel05.add(button30).setVisible(false);
setSize(400, 350);
setVisible(true);
}
public static void main(String[] args) {
JFileCabinet frame = new JFileCabinet();
}
String selection;
class ButtonListener implemen开发者_运维百科ts ActionListener {
private String e;
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
String clicked = null;
if (e.getActionCommand().equals("A")) { //clicked.equals("A");
System.out.println("A");
}
}
}
}
label1.setText("Folder " + e.getActionCommand());
The nudge - and not the full blown answer is this:
- Go and read the public API for JLabel
- Identify the method that sets the text on JLabel
- Instead of checking for an A string value, just set the value of
folder
to the value ofe.getActionCommand()
.
精彩评论