为什么前端不是我想要的方式?

问题描述 投票:0回答:1

我想实现这种外观并尝试相应地设置 JPanel 组件,但当我运行应用程序时,我看到了不同的位置。这就是我想要的样子:

但这就是我所拥有的:

首先,顺序不正确,其次,项目之间没有空格,所以总的来说,这不是我想要的。如何修复它以使其看起来与我第一次添加的图像相似? 我的代码:

package Lab5;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

public class FontDesigner extends JFrame {
    public FontDesigner() {
        // JFrame
        setSize(400, 400);
        setTitle("Font Changer");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // String
        JLabel text = new JLabel("Change Me");
        text.setFont(new Font("Arial", 1, 28));

        // Radio Button
        JRadioButton button1 = new JRadioButton("Small");
        JRadioButton button2 = new JRadioButton("Medium");
        JRadioButton button3 = new JRadioButton("Large");
        ButtonGroup radioGroup = new ButtonGroup();
        radioGroup.add(button1);
        radioGroup.add(button2);
        radioGroup.add(button3);

        /*
         * if (button1.isSelected()) {
         * text.setSize(1);
         * }
         */

        // Combo Box
        JComboBox combo = new JComboBox<>();
        combo.addItem("Serif");
        combo.addItem("Sans-Serif");
        // String selectedFont = (String) combo.getSelectedItem();

        // Check Box
        JCheckBox checkBox1 = new JCheckBox("Italic");
        JCheckBox checkBox2 = new JCheckBox("Bold");
        ButtonGroup checkBoxGroup = new ButtonGroup();
        checkBoxGroup.add(checkBox1);
        checkBoxGroup.add(checkBox2);

        // JPanel
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        panel1.setLayout(new BorderLayout());
        panel2.setLayout(new BorderLayout());
        panel3.setLayout(new BorderLayout());
        panel4.setLayout(new BorderLayout());
        panel1.add(text, BorderLayout.CENTER);
        panel2.add(combo, BorderLayout.CENTER);
        panel3.add(button1, BorderLayout.WEST);
        panel3.add(button2, BorderLayout.CENTER);
        panel3.add(button3, BorderLayout.EAST);
        panel4.add(checkBox1);
        panel4.add(checkBox2);

        // Frame Configuration
        // setLayout(new BorderLayout());
        setLayout(new FlowLayout());
        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);

        // Set Visibility
        setVisible(true);

    }

    public static void main(String[] args) {
        FontDesigner newFontDesigner = new FontDesigner();
    }
}
java swing jframe jpanel
1个回答
0
投票

简介

我重新排列了您的代码以创建以下 GUI。

说明

Oracle 有一个有用的教程,使用 Swing 创建 GUI。跳过使用 NetBeans IDE 学习 Swing 部分。

所有 Swing 应用程序都必须以调用

SwingUtilities

 
invokeLater
 方法开始。此方法确保 Swing 组件在 
Event Dispatch Thread 上创建并执行。

我注意到的第一件事是你的 GUI 可以分为四个独立的

JPanels

。一根用来容纳文本,一根用来容纳字体选择,一根用来容纳样式,一根用来容纳尺寸。

因此,我将您的代码重新排列为四种方法,每种方法都会生成其中一个

JPanels

然后,我决定文本

JPanel

可能应该位于
JFrame's
BorderLayout
的中心。因此,我创建了一个控件 
JPanel
 来使用另一个 
JPanels
 显示底部三个 
BorderLayout

在单独的方法中创建每个

JPanel

 使我能够尝试不同的 Swing 布局管理器,并看看我最喜欢哪一个。它还使其他人更容易阅读和理解代码。

我修改了

JComboBox

 以保存 
AppFont
 实例。这样,我可以在组合框中显示字体系列名称。当用户选择其中一个选项时,您将有一个 
Font
 实例移动到 
currentFont
 字段。

代码

这是完整的可运行代码。我将附加类设置为内部类,这样我就可以将代码作为一个块发布。

import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; public class FontDesigner implements Runnable { public static void main(String[] args) { SwingUtilities.invokeLater(new FontDesigner()); } private Font currentFont; private JComboBox<AppFont> fontComboBox; private JLabel textLabel; public FontDesigner() { this.currentFont = new Font("Arial", Font.BOLD, 28); } @Override public void run() { JFrame frame = new JFrame("Font Changer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(createDisplayPanel(), BorderLayout.CENTER); frame.add(createControlPanel(), BorderLayout.SOUTH); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } private JPanel createDisplayPanel() { JPanel panel = new JPanel(new FlowLayout()); panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); textLabel = new JLabel("Change Me"); textLabel.setFont(currentFont); panel.add(textLabel); return panel; } private JPanel createControlPanel() { JPanel panel = new JPanel(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); panel.add(createFontSelectionPanel(), BorderLayout.NORTH); panel.add(createStylePanel(), BorderLayout.CENTER); panel.add(createSizePanel(), BorderLayout.SOUTH); return panel; } private JPanel createFontSelectionPanel() { JPanel panel = new JPanel(new FlowLayout()); fontComboBox = new JComboBox<>(); fontComboBox.addItem(new AppFont(new Font("Arial", Font.BOLD, 28))); fontComboBox.addItem(new AppFont(new Font("Dialog", Font.BOLD, 28))); fontComboBox.addItem(new AppFont(new Font("Serif", Font.BOLD, 28))); panel.add(fontComboBox); return panel; } private JPanel createStylePanel() { JPanel panel = new JPanel(new FlowLayout()); panel.setBorder(BorderFactory.createTitledBorder("Style")); JCheckBox checkBox2 = new JCheckBox("Bold"); panel.add(checkBox2); JCheckBox checkBox1 = new JCheckBox("Italic"); panel.add(checkBox1); return panel; } private JPanel createSizePanel() { JPanel panel = new JPanel(new FlowLayout()); panel.setBorder(BorderFactory.createTitledBorder("Size")); ButtonGroup radioGroup = new ButtonGroup(); JRadioButton button1 = new JRadioButton("Small"); radioGroup.add(button1); panel.add(button1); JRadioButton button2 = new JRadioButton("Medium"); radioGroup.add(button2); panel.add(button2); JRadioButton button3 = new JRadioButton("Large"); radioGroup.add(button3); panel.add(button3); return panel; } public class AppFont { private final Font font; private final String fontName; public AppFont(Font font) { this.font = font; this.fontName = font.getFamily(); } public Font getFont() { return font; } @Override public String toString() { return fontName; } } }
    
© www.soinside.com 2019 - 2024. All rights reserved.