JPanel.setSize() 不起作用
看看这段代码,朋友们不知道为什么我无法设置 JPanel 的大小... 。
import java.io.*;
import java.util.*;
import java.sql.*;
//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="k" height=400 width=400></applet>*/
public class k extends JApplet implements ActionListener
{
JButton b,tinfo;
JLabel l1,l2;
JTextField f1,f2;
JPanel p1;
CardLayout c1;
public void init()
{
b=new JButton("submit");
f1=new JTextField(20);
f2=new JTextField(20);
l1=new JLabel("username");
l2=new JLabel("password");
p1=new JPanel();
c1=new CardLayout();
add(l1);
add(f1);
add(l2);
add(f2);
add(b);
add(p1);
setLayout(new FlowLayout());
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection con=DriverManager.getConnection("jdbc:odbc:mohit:","system","rock");
try
{
Properties p=new Properties();
p.load(new FileInputStream("mohu.properties"));
String str1=f1.getText();
String str2=p.getProperty("username");
System.out.println(str1);
System.out.println(str2);
//System.out.println(Integer.toString(str2.length()));
if(str1.equals(str2))
{
if((f2.getText()).equals(p.getProperty("password")))
{
System.out.println("you are entered");
}
else
{
System.out.println("wrong first");
}
}
else
{
System.out.println("wrong second ");
}
tinfo=new JButton("teachers information");
p1.add(tinfo);
p1.setSize(200,200);
p1.setBackground(Color.RED);
//tinfo.setSize(50,50);
p1.setLayout(c1);
c1.next(p1);
}
catch(Exception ee)
{
ee.printStackTrace();
System.out.println("Exception caught ");
}
}
public void paint()
{}
}
Check out this code friends don't know why i am not able to set the size of JPanel...
.
import java.io.*;
import java.util.*;
import java.sql.*;
//import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="k" height=400 width=400></applet>*/
public class k extends JApplet implements ActionListener
{
JButton b,tinfo;
JLabel l1,l2;
JTextField f1,f2;
JPanel p1;
CardLayout c1;
public void init()
{
b=new JButton("submit");
f1=new JTextField(20);
f2=new JTextField(20);
l1=new JLabel("username");
l2=new JLabel("password");
p1=new JPanel();
c1=new CardLayout();
add(l1);
add(f1);
add(l2);
add(f2);
add(b);
add(p1);
setLayout(new FlowLayout());
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection con=DriverManager.getConnection("jdbc:odbc:mohit:","system","rock");
try
{
Properties p=new Properties();
p.load(new FileInputStream("mohu.properties"));
String str1=f1.getText();
String str2=p.getProperty("username");
System.out.println(str1);
System.out.println(str2);
//System.out.println(Integer.toString(str2.length()));
if(str1.equals(str2))
{
if((f2.getText()).equals(p.getProperty("password")))
{
System.out.println("you are entered");
}
else
{
System.out.println("wrong first");
}
}
else
{
System.out.println("wrong second ");
}
tinfo=new JButton("teachers information");
p1.add(tinfo);
p1.setSize(200,200);
p1.setBackground(Color.RED);
//tinfo.setSize(50,50);
p1.setLayout(c1);
c1.next(p1);
}
catch(Exception ee)
{
ee.printStackTrace();
System.out.println("Exception caught ");
}
}
public void paint()
{}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 p1.setPreferredSize(new Dimension(200,200))
Try using p1.setPreferredSize(new Dimension(200,200))
它不起作用,因为窗口具有灵活的大小值。它们可以拉伸和收缩,因此您可以将首选尺寸设置为默认尺寸。
It doesn't work because windows have a flexible size value. The can stretch and shrink, so you set a PREFERRED size as a kind of default.