当前位置: 技术问答>java相关
高手请进,一个问题问您。
来源: 互联网 发布时间:2015-11-11
本文导语: 下面的代码运行结果JScrollPane面板或者是JTable占空间过大,不能调整大小,如何调整到自己想要的大小,请高手指点 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.*; p...
下面的代码运行结果JScrollPane面板或者是JTable占空间过大,不能调整大小,如何调整到自己想要的大小,请高手指点
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class client_test
{
public static void main(String[] args)
{
ClientTest aClientTest = new ClientTest();
aClientTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aClientTest.show();
}
}
class ClientTest extends JFrame
{
public ClientTest()
{
setTitle("ClientTest");
setSize(WIDTH,HEIGHT);
JMenu fileMenu =new JMenu("File");
JMenuItem exitItem = fileMenu.add(new AbstractAction("Exit"){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
ClientTestPane clientTestPane = new ClientTestPane();
Container contenPaner = getContentPane();
contenPaner.add(clientTestPane);
}
public static final int WIDTH = 500;
public static final int HEIGHT =520;
}
class ClientTestPane extends JPanel
{
public ClientTestPane()
{
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched,"Test");
setBorder(titled);
JTable jTable = new JTable(4,4);
JScrollPane jScrollPane = new JScrollPane(jTable);
add(jScrollPane);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class client_test
{
public static void main(String[] args)
{
ClientTest aClientTest = new ClientTest();
aClientTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aClientTest.show();
}
}
class ClientTest extends JFrame
{
public ClientTest()
{
setTitle("ClientTest");
setSize(WIDTH,HEIGHT);
JMenu fileMenu =new JMenu("File");
JMenuItem exitItem = fileMenu.add(new AbstractAction("Exit"){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
ClientTestPane clientTestPane = new ClientTestPane();
Container contenPaner = getContentPane();
contenPaner.add(clientTestPane);
}
public static final int WIDTH = 500;
public static final int HEIGHT =520;
}
class ClientTestPane extends JPanel
{
public ClientTestPane()
{
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched,"Test");
setBorder(titled);
JTable jTable = new JTable(4,4);
JScrollPane jScrollPane = new JScrollPane(jTable);
add(jScrollPane);
}
}
|
.
.
.
JTable jTable = new JTable(4,4);
JScrollPane jScrollPane = new JScrollPane(jTable);
jScrollPane.setPreferredSize(new Dimension(400,200));//加上这句代码
add(jScrollPane);
.
.
JTable jTable = new JTable(4,4);
JScrollPane jScrollPane = new JScrollPane(jTable);
jScrollPane.setPreferredSize(new Dimension(400,200));//加上这句代码
add(jScrollPane);