画板问题
如何是画板上的控件随着窗口的最大画而变大
这个问题解决方法有多种
第一种,是布局问题。你可以选择适当的layout,或者自己定义一个layout来布置你的控件。
第二种,在面板上增加一个resize监听器,在监听器中设置控件的大小
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyFieldExample extends JFrame implements ActionListener { JLabel JL1,JL2,JL3; JButton JB1,JB2; JTextField JF1; JPasswordField JP1; public MyFieldExample() { super("登录界面"); JL1=new JLabel("用户名称"); JL1.setBounds(15,5,60,20); JL2=new JLabel("登录密码"); JL2.setBounds(15,25,60,20); JF1=new JTextField("",20); JF1.setBounds(110,5,100,20); JP1=new JPasswordField("",20); JP1.setBounds(110,25,100,20); JB1=new JButton("确定"); JB1.setBounds(35,50,60,30); JB1.addActionListener(this); JB2=new JButton("取消"); JB2.setBounds(130,50,60,30); JB2.addActionListener(this); JL3=new JLabel(""); JL3.setBounds(60,90,200,30); Container winContainer=this.getContentPane(); winContainer.setLayout(null); winContainer.add(JL1); winContainer.add(JL2); winContainer.add(JB1); winContainer.add(JB2); winContainer.add(JL3); winContainer.add(JF1); winContainer.add(JP1); this.setSize(250,150); this.setVisible(true); } public static void main(String args[]) { MyFieldExample w1=new MyFieldExample(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==JB1) { if(JF1.getText().equals("JAVA")&&JP1.getText().equals("1234")) JL3.setText("输入正确,登录成功"); else JL3.setText("输入错误,请重新输入"); } else if (e.getSource()==JB2) System.exit(0); } }帮忙看看,看怎么能最大化的时候,控件也跟着变换
import java.awt.Container; import java.awt.import java.awt.import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class B_H_J extends JFrame { JLabel JL1, JL2, JL3; JButton JB1, JB2; JTextField JF1; JPasswordField JP1; public B_H_J() { super("登录界面"); this.setSize(250, 150); JB1 = new JButton("确定"); JB1.setBounds(35, 50, 60, 30); Container winContainer = this.getContentPane(); winContainer.setLayout(null); winContainer.add(JB1); winContainer.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { JB1.setBounds(15, 5, (int) (getSize().width * 0.4), (int) (getSize().height * 0.4)); } }); this.setVisible(true); } public static void main(String args[]) { B_H_J w1 = new B_H_J(); } }