目前,我正在为一个研究项目工作,但是我有一个问题,我既找不到解决方案,也无法理解问题。

我用Java语言制作了一个响应式计算器,每个键都响应式。但是,我的显示区域(JLabel)无法填充我的窗口的水平位置。如果我增加它的大小,那所有的列都会改变它的大小! > <

有人能帮我吗?

import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class CalculatorUI {
static JButton designButton(GridBagConstraints gbc, String text)
{
     JButton button = new JButton(text);
     ImageIcon image;
     Border withoutBorder = new LineBorder(new Color(75, 75, 75), 1, true);
     button.setBorder(withoutBorder);
     button.setFont(new Font("Gill Sans MT", Font.BOLD, 40));
     button.setBackground(new java.awt.Color(23,23,23));
     gbc.weightx = 0.5;
     gbc.weighty = 0.5;
     gbc.fill = GridBagConstraints.BOTH;
     gbc.gridwidth = 1;
     switch (text) {
     case "sqr":
         button.setText("\u221A");
         button.setForeground(new Color(150, 150, 150));
         break;
     case "sq":
         button.setText("x²");
         button.setForeground(new Color(150, 150, 150));
         break;
     case "1x":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\1x.png");
         button.setText(null);
         button.setIcon(image);
         break;
     case "DEL":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\del.png");
         button.setText(null);
         button.setIcon(image);
         break;
     case "DIV":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\div.png");
         button.setText(null);
         button.setIcon(image);
         break;
     // CS -> Change sign !
     case "CS":
         image = new ImageIcon("C:\\Users\\Emixam23\\Documents\\Workspace\\SimpleCount\\assets\\cs.png");
         button.setText(null);
         button.setIcon(image);
         break;
     default:
         button.setForeground(new Color(150, 150, 150));
         break;
    }
     return (button);
}

static JLabel designTextField(GridBagConstraints gbc)
{
    JLabel display = new JLabel("---------------");
    Border withoutBorder = new LineBorder(new Color(0, 0, 0), 0, true);
    display.setBorder(withoutBorder);
    //display.setFont(new Font("Gill Sans MT", Font.BOLD, 40));
    gbc.fill = GridBagConstraints.NORTH;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 0;
    return (display);
}

public static void makeSimpleGUI(Container pane) {
    JButton button;

    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    //Add of jlabel to display results
    pane.add(designTextField(gbc));

    String[] keys = {"%", "sqr", "sq", "1x", "CE", "C", "DEL", "DIV", "7", "8", "9", "X", "4", "5", "6", "-", "1", "2", "3", "+", "CS", "0", ".", "="};
    for (int i = 0; i < keys.length; i++)
    {
        button = designButton(gbc, keys[i]);
        gbc.gridx = i % 4;
        gbc.gridy = 1 + (i / 4);
        pane.add(button, gbc);
    }
}

public static void CalculatorGUI() {
    JFrame frame = new JFrame("SimpleCount");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    makeSimpleGUI(frame.getContentPane());
    frame.setMinimumSize(new Dimension(400, 400));
    frame.setVisible(true);
}

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

最佳答案

您可以使用...将JLabel添加到面板中。

pane.add(designTextField(gbc));


但这并不适用GridBagConstraints,该方法已将方法修改过的GridBagConstraints.NORTH应用于标签,相反,您应该做更多类似的事情...

pane.add(designTextField(gbc), gbc);


但是,我不鼓励这样做,您的方法应该没有副作用,它应该做一个很好的工作

static JLabel designTextField()
{
    JLabel display = new JLabel("---------------");
    Border withoutBorder = new LineBorder(new Color(0, 0, 0), 0, true);
    display.setBorder(withoutBorder);
    //display.setFont(new Font("Gill Sans MT", Font.BOLD, 40));
    return (display);
}

public static void makeSimpleGUI(Container pane) {
    JButton button;

    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.weightx = 0.5;
    gbc.gridx = 0;
    gbc.gridy = 0;

    //Add of jlabel to display results
    pane.add(designTextField(), gbc);


此外,JLabel是锚点属性,而不是填充属性

因为LineBorder和按钮共享相同的空间(列),所以您需要允许标签跨越多列。

加...

gbc.gridwidth = GridBagConstraints.REMAINDER;


约束

java - 使用Java Swing JLabel进行扩展的效果不佳-LMLPHP

我使用了带有红色的,因此可以直观地看到布局

关于java - 使用Java Swing JLabel进行扩展的效果不佳,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33680865/

10-10 18:02