如何将组件添加到JPanel [英] How to add Components into a JPanel

查看:153
本文介绍了如何将组件添加到JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在面板中添加一些组件并设置面板背景颜色.但是我做不到.谁能建议我,怎么做?这是我的代码.

I Just want to add few components into a panel and set the panel background color.But I can't make it. Can anyone suggest me , how to do it?? Here is my Code.

public Multiple2() {
        getContentPane().setLayout(null);

        JPanel p1 = new JPanel();
        p1.setBackground(Color.RED);
        getContentPane().add(p1,BorderLayout.SOUTH);

        lb1 = new JLabel("Enter the First Number: ");
        lb1.setBounds(10, 10, 250, 20);

        tf1 = new JTextField(100);
        tf1.setBounds(155, 10, 400, 20);

        lb2 = new JLabel("Enter the Second Number: ");
        lb2.setBounds(10, 35, 250, 20);

        tf2 = new JTextField(100);
        tf2.setBounds(155, 35, 400, 20);

        getContentPane().add(lb1);
        getContentPane().add(tf1);
        getContentPane().add(lb2);
        getContentPane().add(tf2);


        setVisible(true);
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

推荐答案

您似乎正在尝试将p添加到contentPane的BorderLayout.SOUTH位置,但您已删除了contentPane的布局管理器 >,因此它没有SOUTH位置,因此您永远不会在任何地方看到p1.

You appear to be trying to add p to the contentPane's BorderLayout.SOUTH location, but you've removed the contentPane's layout manager, and so it has no SOUTH location, and so you never see p1 anywhere.

要将组件添加到p1 JPanel,您需要像使用JFrame的contentPane一样使用add(...)方法.因此,而不是

To add components to the p1 JPanel, you need to use the add(...) method just as you're doing with the JFrame's contentPane. So instead of

getContentPane().add(foo);

您会这样做:

p1. add(foo);

然后,您可能需要将p1 JPanel添加到contentPane的BorderLayout.CENTER位置,而不使用null布局.

Then you'd possibly need to add the p1 JPanel to the contentPane's BorderLayout.CENTER position, and not use null layout.

空布局和setBounds()似乎是Swing新手创建复杂GUI的最简单和最佳方法,但您创建的Swing GUI越多,使用它们时就会遇到的困难就越大.当GUI调整大小时,它们不会调整组件的大小;它们是要增强或维护的皇家女巫;放置在滚动窗格中时,它们会完全失败;在所有平台或与原始分辨率不同的屏幕分辨率下查看时,它们看起来都是令人毛骨悚然的

While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

例如:

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;

public class Mult2 extends JPanel {
    private JTextField field1 = new JTextField(10);
    private JTextField field2 = new JTextField(10);

    public Mult2() {
        setLayout(new GridBagLayout());

        add(new JLabel("Enter the First Number:"), createGbc(0, 0));
        add(field1, createGbc(1, 0));
        add(new JLabel("Enter the Second Number:"), createGbc(0, 1));
        add(field2, createGbc(1, 1));

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setBackground(Color.PINK);
    }

    private static GridBagConstraints createGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        int right = x % 2 == 0 ? 15 : 5;
        gbc.insets = new Insets(5, 5, 5, right);
        return gbc;
    }

    private static void createAndShowGui() {
        Mult2 mainPanel = new Mult2();

        JFrame frame = new JFrame("Multiply");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

这篇关于如何将组件添加到JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆