Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPanel.setSize()不工作_Java_Swing_Awt_Null Layout Manager - Fatal编程技术网

Java JPanel.setSize()不工作

Java JPanel.setSize()不工作,java,swing,awt,null-layout-manager,Java,Swing,Awt,Null Layout Manager,查看此代码朋友不知道为什么我无法设置JPanel的大小。。。 import java.io.*; 导入java.util.*; 导入java.sql.*; //导入java.applet.*; 导入java.awt.*; 导入java.awt.event.*; 导入javax.swing.*; /**/ 公共类k扩展了JApplet实现ActionListener { JButton b,tinfo; JLabel l1,l2; JTextField f1,f2; JPanel-p1; 卡片布

查看此代码朋友不知道为什么我无法设置JPanel的大小。。。

import java.io.*;
导入java.util.*;
导入java.sql.*;
//导入java.applet.*;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
/**/
公共类k扩展了JApplet实现ActionListener
{
JButton b,tinfo;
JLabel l1,l2;
JTextField f1,f2;
JPanel-p1;
卡片布局c1;
公共void init()
{
b=新按钮(“提交”);
f1=新的JTextField(20);
f2=新的JTextField(20);
l1=新的JLabel(“用户名”);
l2=新的JLabel(“密码”);
p1=新的JPanel();
c1=新的CardLayout();
添加(l1);
添加(f1);
添加(l2);
添加(f2);
添加(b);
添加(p1);
setLayout(新的FlowLayout());
b、 addActionListener(此);
}
已执行的公共无效行动(行动事件ae)
{
//forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
//Connection con=DriverManager.getConnection(“jdbc:odbc:mohit:,“system”,“rock”);
尝试
{
属性p=新属性();
p、 加载(新文件输入流(“mohu.properties”);
字符串str1=f1.getText();
字符串str2=p.getProperty(“用户名”);
系统输出打印项次(str1);
系统输出打印项次(str2);
//System.out.println(Integer.toString(str2.length());
if(str1等于(str2))
{
if((f2.getText()).equals(p.getProperty(“密码”))
{
System.out.println(“您已输入”);
}
其他的
{
System.out.println(“错误优先”);
}
}
其他的
{
System.out.println(“错误秒”);
}
tinfo=新的JButton(“教师信息”);
p1.添加(tinfo);
p1.设置大小(200200);
p1.立根背景(颜色:红色);
//tinfo.设置尺寸(50,50);
p1.设置布局(c1);
c1.下一个(p1);
}
捕获(异常ee)
{
ee.printStackTrace();
System.out.println(“捕获异常”);
}
}
公共空间涂料()
{}
}

尝试使用p1.setPreferredSize(新维度(200200))

因为windows有一个灵活的大小值,所以它不起作用。可以拉伸和收缩,因此可以将首选尺寸设置为默认尺寸

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()
    {}
}