Java如何把表格和按钮放在一个窗口中
packagecom.lzw;importjava.awt.*;importjavax.swing.*;publicclassMainextendsJFrame{priv...
package com.lzw;
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private static final long serialVersionUID=1L;
public Main() {
Container c=getContentPane();
setVisible(true);
setBounds(100,100,700,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTable");
String[] columnNames={"名称","数量","单价"};
String[][] tableValues={{" "," "," "},{" "," "," "},{" "," "," "},{" "," "," "},{" "," "," "},{" "," "," "}};
final JTable t=new JTable(tableValues, columnNames);
t.setBounds(0,0,700,200);
JButton figurebtn =new JButton("Figure...");
figurebtn.setBounds(300,100,40,10);
JPanel p=new JPanel();
p.add(figurebtn);
final JScrollPane sp=new JScrollPane(t);
c.add(sp,BorderLayout.CENTER);
c.add(p);
}
public static void main(String[] args) {
new Main();
}
}
如果添加setLayout(null);,表格无法显示;
如果不加,用c.add(figurebtn);,按钮会显示在整个窗口中;
按照上面的代码,窗口里只有一个按钮;
如果在面板中添加表格和按钮,再把面板添加到滚动面板中,表格会显示不完全。
请问怎么解决??? 展开
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private static final long serialVersionUID=1L;
public Main() {
Container c=getContentPane();
setVisible(true);
setBounds(100,100,700,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTable");
String[] columnNames={"名称","数量","单价"};
String[][] tableValues={{" "," "," "},{" "," "," "},{" "," "," "},{" "," "," "},{" "," "," "},{" "," "," "}};
final JTable t=new JTable(tableValues, columnNames);
t.setBounds(0,0,700,200);
JButton figurebtn =new JButton("Figure...");
figurebtn.setBounds(300,100,40,10);
JPanel p=new JPanel();
p.add(figurebtn);
final JScrollPane sp=new JScrollPane(t);
c.add(sp,BorderLayout.CENTER);
c.add(p);
}
public static void main(String[] args) {
new Main();
}
}
如果添加setLayout(null);,表格无法显示;
如果不加,用c.add(figurebtn);,按钮会显示在整个窗口中;
按照上面的代码,窗口里只有一个按钮;
如果在面板中添加表格和按钮,再把面板添加到滚动面板中,表格会显示不完全。
请问怎么解决??? 展开
1个回答
展开全部
c.add(figurebtn,BorderLsyllayout.SOUTH)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询