import java.awt.*;
import javax.swing.*;public class Test {
public static void main(String[] args) {
new TestFrame();
}
}class TestFrame extends JFrame{
private static final long serialVersionUID = 1L;
JTextField jt;
JButton jb;
JLabel jl;
public TestFrame(){
super("TestFrame"); Container container=getContentPane();
jt=new JTextField(35);
jb=new JButton("清空");
jl=new JLabel();
JPanel p=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel(); p1.setLayout(null);
jb.setSize(60, 25);
jb.setLocation(50, 0);
p2.setLayout(null);
jl.setSize(85, 30);
jl.setLocation(50, 0);
jl.setText("显示设置"); p.add(jt);
p1.add(jb);
p2.add(jl);
container.add(p,BorderLayout.NORTH);
container.add(p1,BorderLayout.CENTER);
container.add(p2,BorderLayout.SOUTH);
setLocation(0, 50);
setSize(500,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
为什么运行时显示不出来jl设置的文字呢?
import javax.swing.*;public class Test {
public static void main(String[] args) {
new TestFrame();
}
}class TestFrame extends JFrame{
private static final long serialVersionUID = 1L;
JTextField jt;
JButton jb;
JLabel jl;
public TestFrame(){
super("TestFrame"); Container container=getContentPane();
jt=new JTextField(35);
jb=new JButton("清空");
jl=new JLabel();
JPanel p=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel(); p1.setLayout(null);
jb.setSize(60, 25);
jb.setLocation(50, 0);
p2.setLayout(null);
jl.setSize(85, 30);
jl.setLocation(50, 0);
jl.setText("显示设置"); p.add(jt);
p1.add(jb);
p2.add(jl);
container.add(p,BorderLayout.NORTH);
container.add(p1,BorderLayout.CENTER);
container.add(p2,BorderLayout.SOUTH);
setLocation(0, 50);
setSize(500,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
为什么运行时显示不出来jl设置的文字呢?
和
jl.setLocation(50, 0);
是不是有问题,应该会覆盖吧
import java.awt.*;
import javax.swing.*;public class Test {
public static void main(String[] args) {
new TestFrame();
}
}class TestFrame extends JFrame {
private static final long serialVersionUID = 1L; JTextField jt; JButton jb; JLabel jl; public TestFrame() {
super("TestFrame");
Container container = getContentPane();
jt = new JTextField(35);
jb = new JButton("清空");
jl = new JLabel();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel(); p1.setLayout(null);
jb.setSize(60, 25);
jb.setLocation(300, 0);
p2.setLayout(null);
jl.setSize(85, 30);
jl.setLocation(50, 0);
jl.setText("显示设置");
p.add(jt);
p1.add(jb);
p1.add(jl);
container.add(p, BorderLayout.NORTH);
container.add(p1, BorderLayout.CENTER);
container.add(p2, BorderLayout.SOUTH);
setLocation(0, 50);
setSize(500, 120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这样就显示了 , 我想应该是布局的原因吧
p2.setLayout(null);
上面这两句有问题,改为如下就可以了
import java.awt.*; import javax.swing.*; public class Test {
public static void main(String[] args) {
new TestFrame();
}
} class TestFrame extends JFrame{
private static final long serialVersionUID = 1L;
JTextField jt;
JButton jb;
JLabel jl; public TestFrame(){
super("TestFrame"); Container container=getContentPane();
jt=new JTextField(35);
jb=new JButton("清空");
jl=new JLabel();
JPanel p=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel(); p1.setLayout(new FlowLayout());
jb.setSize(60, 25);
jb.setLocation(50, 0);
p2.setLayout(new FlowLayout());
jl.setSize(85, 30);
jl.setLocation(50, 0);
jl.setText("显示设置"); p.add(jt);
p1.add(jb);
p2.add(jl);
container.add(p,BorderLayout.NORTH);
container.add(p1,BorderLayout.CENTER);
container.add(p2,BorderLayout.SOUTH);
setLocation(0, 50);
setSize(500,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import javax.swing.*;public class Test {
public static void main(String[] args) {
TestFrame t = new TestFrame();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}class TestFrame extends JFrame { public TestFrame() {
setLocation(0, 50);
setSize(500, 120);
TestPanel tp = new TestPanel();
add(tp);
}
}
class TestPanel extends JPanel{
public TestPanel(){
setLayout(null);
JTextField jt = new JTextField(25);
JButton jb = new JButton("清空");
JLabel jl = new JLabel();
jt.setSize(300,20);
jt.setLocation(95, 10);
jb.setSize(85, 20);
jb.setLocation(10,50);
jl.setSize(85, 20);
jl.setLocation(10,10);
jl.setText("显示设置");
add(jt);
add(jb);
add(jl); }
}
用一个面板 的绝对定位不行吗?
你看看 这个代码 如果 不行 你就找高人吧
p.add(jt);
p.add(jb);
p.add(jl);
这样也可以实现的