在按钮的相对位置添加JLabel

时间:2014-02-07 03:40:30

标签: java swing jbutton jlabel layout-manager

好的,这是我到目前为止的相关代码。

  for (int i = gameLines - 1; i > 0; i--)
  {
     for (int j = 0; j < i; j++)
     {
        JButton jButton = new JButton();
        jButton.setSize(buttonWidth, buttonHeight);
        jButton.setText("" + line[i].charAt(j));
        int x = ((buttonWidth / 2 * gameLines + 1) - (buttonWidth / 2 * i) + (buttonWidth * j));
        int y = (gameLines - (i + 1)) * buttonHeight;
        jButton.setLocation(x, y);
        panel.add(jButton);
        button[i][j] = jButton;
        button[i][j].setActionCommand(Integer.toString(i) + "." + Integer.toString(j));
        button[i][j].addActionListener(this);
     }
  }

代码创建并将我的所有按钮放在我想要的位置。我花了一段时间才弄明白。我原来是AppleSoft BASIC程序员,对于i&amp; j变量名。

现在为了好玩。在底部3行按钮的右侧,我想放置一个jLabel。 jLabel的右边缘与顶行中最右边按钮的右边缘对齐。每个中的文本都是右对齐的,以:以及最多4位数字结尾。

我想我可以简单地计算位置,就像按钮位置一样。文本,我打算根据开关/案例添加。

我在理解JLabels时遇到了麻烦,所以我很感激能得到的任何帮助。

我目前的想法是:在j循环之后插入

if (i < 4)
{
    JLabel jLable = new JLabel();
    JLabel.setSize(???, buttonHeight);
    Calculate value of X;
    int y = (gameLines - (i +1)) * buttonHeight;
    jLabel.setLocation(x,y);
    switch (i)
    {
       case 3:
          jLabel.setText("Buttons Clicked: " + buttonsClicked);
          break;
       case 2:
          etc.
    }
    panel.add(jLabel);
}

请帮助

1 个答案:

答案 0 :(得分:6)

出于一些原因,我会避免绝对布局。在其他PC上运行它的那一刻,整个事情开始分崩离析,相反,你应该依赖于Swing中可用的布局管理器API。

以下示例使用复合布局方法。每行都是它自己的容器(JPanel),然后每行都会添加到主面板中。

Prymid

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonPyramid {

    public static void main(String[] args) {
        new ButtonPyramid();
    }

    public ButtonPyramid() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private String[] lines;

        public TestPane() {
            lines = new String[]{
                "AAFITQPNXBE",
                "?AXOPKMSUR",
                "TGKFREUDI",
                "DFEAAEOY",
                "ZDE?VIF",
                "G@RMLC",
                "YUJGO",
                "NSCP",
                "KO@",
                "MI",
                "Y",
                "B",
            };

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.gridy = 1;
            gbc.gridx = 0;
            for (String line : lines) {
                JPanel panel = new JPanel(new GridBagLayout());
                for (char ch : line.toCharArray()) {
                    JButton btn = new JButton(Character.toString(ch));
                    btn.setMargin(new Insets(10, 10, 10, 10));
                    panel.add(btn);
                }
                add(panel, gbc);
                gbc.gridy++;
            }
            JLabel label = new JLabel(":1234");
            gbc.gridy -= 3;
            gbc.gridx++;
            gbc.anchor = GridBagConstraints.NORTH;
            add(label, gbc);
        }
    }

}

如果您希望文本不占用另一列,可以尝试一些小技巧,将标签布局约束更改为以下内容......

JLabel label = new JLabel(":1234");
gbc.gridy -= 3;
gbc.anchor = GridBagConstraints.NORTHEAST;
add(label, gbc);

查看Laying out Components within a Container的想法和详情

           lines = new char[][]{...};

           for (int outter = 0; outter < lines.length; outter++) {
                JPanel panel = new JPanel(new GridBagLayout());
                for (int inner = 0; inner < lines[outter].length) {
                    char ch = lines[outter][inner];
                    JButton btn = new JButton(Character.toString(ch));
                    btn.setMargin(new Insets(10, 10, 10, 10));
                    panel.add(btn);
                }
                add(panel, gbc);
                gbc.gridy++;
            }