[英]java object resize on window resize
I've to resize few elements like JTable on window resize. 我必须在窗口调整大小上调整一些元素,例如JTable。 I've been trying this code, but it doesn't work correctly: 我一直在尝试此代码,但无法正常工作:
table.setLocation(0, 23);
Dimension siz = contentPane.getMaximumSize();
table.setSize(siz.height, siz.width - 46);
It resizing my table, but it making it endless, what i don't want. 它调整了我的桌子的大小,但使它无休止,这是我不想要的。 Also I would like to connent scrollbar to this table, and if it's possible - set column width in precentage 我也想将滚动条连接到该表,如果可能的话-以百分比形式设置列宽
Your main problem (with resizing) has more to do with your reliance on form editors then anything to do with Swing or Java 您的主要问题(与调整大小有关)更多地取决于您对表单编辑器的依赖,而不是与Swing或Java有关
Have a look at Laying Out Components Within a Container for more details. 有关更多详细信息,请参见在容器中布置组件 。
You're also don't seem to be utilising a JScrollPane
to house the JTable
in. Have a look at How to Use Tables and How to Use Scroll Panes for more details 您似乎也没有在利用JScrollPane
来容纳JTable
。有关更多详细信息,请参见如何使用表和如何使用滚动窗格 。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class ResizeTest {
public static void main(String[] args) {
new ResizeTest();
}
public ResizeTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
private JButton historyButton;
private JButton otherButton;
public TestPane() {
table = new JTable(new DefaultTableModel(10, 10));
historyButton = new JButton("History");
otherButton = new JButton("Other");
setLayout(new BorderLayout());
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttons.add(historyButton);
buttons.add(otherButton);
add(buttons, BorderLayout.NORTH);
add(new JScrollPane(table));
JPanel footers = new JPanel(new GridLayout(1, 2));
JLabel left = new JLabel("Left");
left.setHorizontalAlignment(JLabel.LEFT);
JLabel right = new JLabel("Right");
right.setHorizontalAlignment(JLabel.LEFT);
footers.add(left);
footers.add(right);
add(footers, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.