问题描述
- java Jpanel的有关问题
-
panButton显示红线,是因为Jpanel的对象只能添加一个button 吗import java.awt.*;import javax.swing.*;public class QQlogin { public static void main(String args[]) { // TODO Auto-generated method stub JFrame w = new JFrame(); w.setSize(250, 125); JLabel labuser = new JLabel("用户名"); JLabel labpass = new JLabel("密码"); JTextField txtuser = new JTextField(); JPasswordField txtpass = new JPasswordField(); JButton btnlogin = new JButton("登录"); JButton btnreg = new JButton("注册"); JButton btncancel = new JButton("取消"); //设置输入面板 JPanel panInput = new JPanel(); panInput.setLayout(new GridLayout(2 , 2)); panInput.add(labuser); panInput.add(txtuser); panInput.add(labpass); panInput.add(txtpass); //设置按键面板 JPanel panButton = new JPanel(); panButton.setLayout(new FlowLayout()); panButton.add(btnlogin); panButoon.add(btnreg); //panButton显示红线 为什么 panButoon.add(btncancel); //panButton显示红线 为什么 //设置窗体 w.setLayout(new BorderLayout()); w.add(panInput,BorderLayout.CENTER); w.add(panButton,BorderLayout.SOUTH); w.setVisible(true); }}
解决方案
加上w.pack();试试呢?