本文介绍了在按钮的相对位置添加JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我到目前为止的相对代码.

OK, this is the relative code I have so far.

  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& j个变量名.

The code creates and places all my buttons where I want them to be. This took me a while to figure out. I'm originally an AppleSoft BASIC programmer, I'm sorry about the i & j variable names.

现在玩的开心.在底部3行按钮的右侧,我想放置一个jLabel. jLabel的右边缘应与第一行中最右边的按钮的右边缘对齐.每个文本都将右对齐,并以:和最多4位数字结尾.

Now for the fun. To the right of the bottom 3 rows of buttons, I want to place a jLabel. The right edge of the jLabel is to align with the right edge of the rightmost button in the top row. The text in each will be right justified, ending with a : and an up-to 4 digit number.

我想我可以像计算按钮位置那样简单地计算位置.文本是基于开关/案例添加的.

I'm thinking I can simply calculate the position like I did the button positions. The text, I was going to add based on a switch/case.

我在理解JLabels时遇到了麻烦,因此,我感谢能获得的任何帮助.

I'm having an trouble understanding JLabels, so I'd appreciate ANY help I can get.

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

My current thoughts are: to be inserted after the j loop

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);
}

请帮助

推荐答案

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

For a bag of reasons, I would avoid absolute layouts. The moment you run it on some other PC, the whole thing begins to fall apart, instead, you should rely on the layout manager API available in Swing.

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

The following example uses a compound layout approach. Each row is it's own container (JPanel) and each row is then added to the main panel.

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);
        }
    }

}

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

If you would prefer the text not to take up another column, there is a little trick you can try, change the label layout constraints to look like the following instead...

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

签出在容器中布置组件想法和细节

           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++;
            }

这篇关于在按钮的相对位置添加JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 17:05
查看更多