java - 如何让 JLabel 显示已选择哪个按钮

标签 java swing jpanel jbutton

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

public class filecabinet extends JFrame implements ActionListener {

    private String folder = "";
    //create buttons

    private JButton a = new JButton("A");
    private JButton b = new JButton("B");
    private JButton c = new JButton("C");
    private JButton d = new JButton("D");
    private JButton e = new JButton("E");
    private JButton f = new JButton("F");
    private JButton g = new JButton("G");
    private JButton h = new JButton("H");
    private JButton i = new JButton("I");
    private JButton j = new JButton("J");
    private JButton k = new JButton("K");
    private JButton l = new JButton("L");
    private JButton m = new JButton("M");
    private JButton n = new JButton("N");
    private JButton o = new JButton("O");
    private JButton p = new JButton("P");
    private JButton q = new JButton("Q");
    private JButton r = new JButton("R");
    private JButton s = new JButton("S");
    private JButton t = new JButton("T");
    private JButton u = new JButton("U");
    private JButton v = new JButton("V");
    private JButton w = new JButton("W");
    private JButton x = new JButton("X");
    private JButton y = new JButton("Y");
    private JButton z = new JButton("Z");
    //create panels
    private JPanel aF = new JPanel();
    private JPanel gL = new JPanel();
    private JPanel mR = new JPanel();
    private JPanel sX = new JPanel();
    private JPanel yZ = new JPanel();
    private JPanel readout = new JPanel();
    private JLabel notify = new JLabel("You have selected folder " + folder);

    public void ComposePane(){
        //set buttons to panels
        aF.add(a);
        aF.add(b);
        aF.add(c);
        aF.add(d);
        aF.add(e);
        aF.add(f);
        //set layout of panels
        aF.setLayout(new GridLayout(2,3));
        gL.add(g);
        gL.add(h);
        gL.add(i);
        gL.add(j);
        gL.add(k);
        gL.add(l);
        gL.setLayout(new GridLayout(2,3));
        mR.add(m);
        mR.add(n);
        mR.add(o);
        mR.add(p);
        mR.add(q);
        mR.add(r);
        mR.setLayout(new GridLayout(2,3));
        sX.add(s);
        sX.add(t);
        sX.add(u);
        sX.add(v);
        sX.add(w);
        sX.add(x);
        sX.setLayout(new GridLayout(2,3));
        yZ.add(y);
        yZ.add(z);
        yZ.add(readout);
        readout.add(notify);
        yZ.setLayout(new GridLayout(2,3));
    }
    public filecabinet(){
        setTitle("File Cabinet");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(5,1));
        ComposePane();
        add(aF);
        add(gL);
        add(mR);
        add(sX);
        add(yZ);
        addActionListener();
    }


    public void actionPerformed(ActionEvent e) {
        folder = ((JButton) e.getSource()).getText();


    }
    public void addActionListener(){
        a.addActionListener(this);
        b.addActionListener(this);
        c.addActionListener(this);
        d.addActionListener(this);
        e.addActionListener(this);
        f.addActionListener(this);
        g.addActionListener(this);
        h.addActionListener(this);
        i.addActionListener(this);
        j.addActionListener(this);
        k.addActionListener(this);
        l.addActionListener(this);
        m.addActionListener(this);
        n.addActionListener(this);
        o.addActionListener(this);
        p.addActionListener(this);
        q.addActionListener(this);
        r.addActionListener(this);
        s.addActionListener(this);
        t.addActionListener(this);
        u.addActionListener(this);
        v.addActionListener(this);
        w.addActionListener(this);
        x.addActionListener(this);
        y.addActionListener(this);
        z.addActionListener(this);

    }
    public static void main(String[]args){
        filecabinet frame = new filecabinet();
        final int WIDTH = 600;
        final int HEIGHT = 600;
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }

}

我宁愿不必为每个按钮都写一个监听器 如果我能得到按钮中包含的文本,我正在徘徊。

如“You have selected foler X”,X是选择的按钮。

最佳答案

同样,使用数组和/或 for 循环来合并您的代码以消除代码冗余。例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class FileCabinet2 extends JPanel {
   public static final char FIRST_LETTER = 'A';
   public static final char LAST_LETTER = 'Z';
   private static final float lARGE_FONT_POINTS = 32f;
   private static final int GRID_COLS = 3;
   private JLabel chosenLabel = new JLabel();

   public FileCabinet2() {
      JPanel letterPanel = new JPanel(new GridLayout(0, GRID_COLS));
      ActionListener btnListener = new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent evt) {
            chosenLabel.setText(evt.getActionCommand());
         }
      };
      for (char c = FIRST_LETTER; c <= LAST_LETTER; c++) {
         String buttonTitle = String.valueOf(c);
         JButton button = new JButton(buttonTitle);
         setFontPoints(button, lARGE_FONT_POINTS);
         letterPanel.add(button);

         button.addActionListener(btnListener);
      }

      JLabel selectionLabel = new JLabel("Selection: ", SwingConstants.RIGHT);
      setFontPoints(selectionLabel, lARGE_FONT_POINTS);
      setFontPoints(chosenLabel, lARGE_FONT_POINTS);


      JPanel bottomPanel = new JPanel(new GridLayout(1, 0));
      bottomPanel.add(selectionLabel);
      bottomPanel.add(chosenLabel);
      bottomPanel.setBorder(BorderFactory.createEtchedBorder());

      setLayout(new BorderLayout());
      add(letterPanel, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   private void setFontPoints(JComponent jComp, float points) {
      jComp.setFont(jComp.getFont().deriveFont(points));
   }

   private static void createAndShowGui() {
      FileCabinet2 mainPanel = new FileCabinet2();

      JFrame frame = new JFrame("File Cabinet");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

现在如果你想改变有多少个按钮列,你需要做的就是改变一个常量,GRID_COLS,同样对于字体大小,只需改变LARGE_FONT_POINTS。

enter image description here

关于java - 如何让 JLabel 显示已选择哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095464/

相关文章:

java - 动态删除 JPanel

java - 从终止当前面板的另一个类(方法)的面板调用方法

java - 未找到带有 URI 的 HTTP 请求的映射 [/springmvc2k/WEB-INF/views/home.jsp]

java - 是否可以在 java 中使用反射创建没有无参数构造函数的类的 'blank' 实例?

java - Subclipse 忽略文件,特别是 .classpath

Java - 在 Jtable 的单元格中添加按钮

Java Web 服务器将 if-modified-since 与上次修改进行比较

contentPanel 内的 Java Swing 调整大小面板

java - 使用 Java Swing 高效重画线条

java - JPanel 未显示