【问题标题】:How do I add JButtons and JLabels without conflicts?如何在不冲突的情况下添加 JButton 和 JLabels?
【发布时间】:2023-12-30 03:03:01
【问题描述】:

当我尝试将 JButton 和 JLabel 添加到 JFrame 中时,它们都相互冲突,其中所有 JButton 都会消失,只有 JLabel 可见。由于某种原因,JLabel 会转到 JFrame 的最左侧,而不是我设置它的所需位置。我是 GUI 相关材料的新手,我愿意从这些错误中吸取教训。

这是我的代码:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Windowb extends JFrame{
    static String title = "This is a JFrame";
    static int width = 500;
    static int height = 400;

    private static final int BUTTON_LOCATION_X = 46;
    private static final int BUTTON_LOCATION_Y = 80;

    public static void main(String[]args){
        Windowb simple = new Windowb(title, width, height);
        JPanel p = new JPanel(); 
        p.setLayout(null);
        JLabel c1 = new JLabel("Name: ");
        JButton b1 = new JButton("Name:");
        JButton b2 = new JButton("Grade:");
        JButton b3 = new JButton("GPA");
        b1.setBounds(BUTTON_LOCATION_X, BUTTON_LOCATION_Y, 90, 20);
        b2.setBounds(50, 170, 90, 20);
        b3.setBounds(50, 240, 90, 20);
        c1.setLocation(100, 250);

        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, "ActionListener is working!");
            }

        });


        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            JOptionPane.showMessageDialog(null, "The second one works too!");
            }
        });

        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, "Surprise!");
            }
        });
        p.add(b1); 
        p.add(b2); 
        p.add(b3);
        simple.add(p);
        simple.add(c1);
    }

    public Windowb(String t, int w, int h){
        setVisible(true);
        setResizable(true);
        setSize(w, h);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(500, 100);
        setTitle(t);
    }
}

【问题讨论】:

  • 因为每个按钮在setBounds 方法中的位置相同。检查您的b1,b2,b3 设置边界。也不要尝试使用空布局,而是与 java 提供的其他布局管理器保持练习。 docs.oracle.com/javase/tutorial/uiswing/layout/using.html
  • 它们都有相同的 x 位置。但不同的 y 位置。唯一显示的是“名称:”,即 JLabel。没有显示任何 Jbuttons。
  • 首先什么是Windowb。您正在尝试在属于 Windowbsimple 对象上添加组件。请遵循一些添加组件的简单规则。请阅读这些文章docs.oracle.com/javase/tutorial/uiswing/start/index.html
  • 问题 #1 您使用的是空布局,请为无休止的问题和问题做好准备,其中,我们不会帮助您解决,因为您应该使用适当的布局,没有这里有理由不这样做;问题 #2 在创建基本 UI 之前,您使窗口变得肮脏。通常,我们建议使用 revalidate 来修复它,但这与布局 api 有关,所以它不会在这里工作
  • 好的,我将布局设置为 setLayout(new BorderLayout()),JButtons 现在出现了,但它仍然不允许我将 JLabel 放置在我想要的位置。

标签: java swing jframe jbutton jlabel


【解决方案1】:

您可能应该使用 LayoutManager。见布局管理器教程:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

setBounds() 被布局管理器用来定位组件。您可以将 LayoutManager 设置为 null 并自己定位组件,但不会以这种方式为您处理诸如调整窗口大小之类的事情(即不会相应地缩放组件和空间)。如果您想保持理智,请不要使用 GridBag 布局中的构建!它会让你发疯!对于更复杂的布局,请使用 http://www.miglayout.com/http://www.jgoodies.com/freeware/libraries/forms/ 。对于简单的布局,请使用 BorderLayout 等布局管理器。

如果您真的不想使用布局管理器,请使用 JPanel。 JFrame 只能容纳一个组件,这就是您的问题。在 JFrame 中放置一个 JPanel,并将您的组件放在 JPanel 中。

【讨论】:

    最近更新 更多