【问题标题】:How to add a JButton to a JFrame?如何将 JButton 添加到 JFrame?
【发布时间】:2018-11-15 18:09:42
【问题描述】:

我尝试向JFrame 添加一个按钮,但由于某种原因它不会出现。如何让它出现?

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.*;

public class GraficoconArreglo extends javax.swing.JFrame {
 JPanel pan = (JPanel) this.getContentPane();
 JLabel []lab = new JLabel[6];
 JTextField []text = new JTextField[6];
 Border border = BorderFactory.createLineBorder(Color.pink,1);
 JButton b = new JButton("Calculate");

public GraficoconArreglo() {
    initComponents();
    pan.setLayout(null);
    pan.setBackground(Color.GRAY);
    for(int i=0; i<lab.length ;i++){
        lab[i] = new JLabel();
        text[i] = new JTextField();

        lab[i].setBounds(new Rectangle(15,(i+1)*40, 60, 25));
        lab[i].setText("Data " + (i+1));
        lab[i].setBorder(border);
        text[i].setBounds(new Rectangle(100,(i+1)*40, 60, 25));

        pan.add(lab[i],null);
        pan.add(text[i],null);

        setSize(200,330);
        setTitle("Arrays in forums.");

        add(b);
        b.addActionListener((ActionListener) this);
    }        
}

【问题讨论】:

  • 永远不要使用空布局。切勿尝试直接致电setBounds。它永远不会起作用。您的按钮可能没有出现,因为它超出了范围。
  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 以最小尺寸提供 ASCII 艺术或 GUI 的 预期 布局的简单绘图,如果可调整大小,则具有更大的宽度和高度 - 以显示应如何使用额外空间。 3) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them ..
  • .. 以及 white space 的布局填充和边框。

标签: java swing jframe jbutton layout-manager


【解决方案1】:

您只创建一个按钮并将其添加到 6 个不同的位置。因此,您只会在最后添加的地方看到它。

【讨论】:

    【解决方案2】:

    您应该将按钮添加到 contentPane,而不是 JFrame。 Eclipse Marketplace 的 SwingDesigner 提供的工作代码如下:

    public class Window extends JFrame {
    
    private JPanel contentPane;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window frame = new Window();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Window() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);
    
        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(170, 110, 89, 23);
        contentPane.add(btnNewButton);
    }
    

    }

    【讨论】:

    • (1-) 不要使用空布局。 Swing 旨在与布局管理器一起使用。 You should add the button to the contentPane, not the JFrame. - 您可以将按钮添加到框架中。框架的 add() 方法会将组件转发到框架的内容窗格。无需将内容窗格专门设置为面板,因为默认的内容窗格已经是 JPanel。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    相关资源
    最近更新 更多