作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试以一种方式进行编码,当用户单击一个按钮时,新的 jlabel 行和 jtextfield 将被添加到 jframe 的网格布局中。像这样:
假设JFrame是800x600,每行的高度是50,当我到达第13行后,我想添加一个垂直滚动条。但是现在我似乎无法按照我的意愿正确添加组件,我也尝试了其他布局,但显然没有成功。
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
public class Testing extends JFrame implements ActionListener
{
public static int x =0;
JPanel panel;
public Testing()
{
super("Add component on JFrame at runtime");
setLayout(new BorderLayout());
panel=new JPanel();
panel.setPreferredSize(new Dimension(800,600));
panel.setAutoscrolls(true);
panel.setLayout(new GridLayout(0,2,0,20));
add(panel,BorderLayout.CENTER);
JButton button=new JButton("CLICK HERE");
add(button,BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
pack();
}
public void actionPerformed(ActionEvent evt)
{
JLabel jlbl1 = new JLabel("Row"+x);
JTextField jtf =new JTextField(10);
jtf.setMaximumSize(new Dimension(400,50));
jlbl1.setMaximumSize(new Dimension(400,50));
panel.add(jlbl1);
panel.add(jtf);
panel.revalidate();
x++;
validate();
}
public static void main(String[]args)
{
Testing test=new Testing();
}
}
编辑:感谢和支持 MadProgrammer 显示指南,这就是我想要的,以防万一其他人遇到与我相同的问题:
感谢 MadProgrammer 和 Andrew Thompson 提供指南。经过一些调整后,这就是我想要的。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public GridBagConstraints c = new GridBagConstraints();
private JPanel fieldsPanel;
private int row;
public TestPane() {
c.gridx =0;
c.gridy =0;
c.fill = GridBagConstraints.NONE;
setLayout(new BorderLayout());
fieldsPanel = new JPanel(new GridBagLayout());
add(new JScrollPane(fieldsPanel));
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fieldsPanel.add(new JLabel("Row " + (++row)),c);
c.gridx++;
fieldsPanel.add(new JTextField(10),c);
fieldsPanel.revalidate();
c.gridy++;
c.gridx--;
}
});
add(btn, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
最佳答案
JTable
相反和/或JScrollPane
并让它为您处理……使用 JTable
示例更新
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
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 TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private DefaultTableModel model;
public TestPane() {
setLayout(new BorderLayout());
model = new DefaultTableModel(
new Object[]{"Row", "Value"},
0);
JTable table = new JTable(model);
add(new JScrollPane(table));
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = model.getRowCount();
model.addRow(new Object[]{count + 1, ""});
}
});
add(btn, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
更新了 JScrollPane
示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel fieldsPanel;
private int row;
public TestPane() {
setLayout(new BorderLayout());
fieldsPanel = new JPanel(new GridLayout(0, 2));
add(new JScrollPane(fieldsPanel));
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fieldsPanel.add(new JLabel("Row " + (++row)));
fieldsPanel.add(new JTextField(10));
fieldsPanel.revalidate();
}
});
add(btn, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
更新了 GridBagLayout
示例
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel fieldsPanel;
private JPanel filler;
private int row;
public TestPane() {
setLayout(new BorderLayout());
filler = new JPanel();
fieldsPanel = new JPanel(new GridBagLayout());
add(new JScrollPane(fieldsPanel));
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fieldsPanel.remove(filler);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = row;
gbc.fill = GridBagConstraints.HORIZONTAL;
fieldsPanel.add(new JLabel("Row " + (++row)), gbc);
gbc.gridx++;
gbc.weightx = 1;
fieldsPanel.add(new JTextField(10), gbc);
gbc.gridy++;
gbc.weighty = 1;
fieldsPanel.add(filler, gbc);
fieldsPanel.revalidate();
}
});
add(btn, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
关于java - 如何将 JComponent 添加到指定大小和位置的 JFrame 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602531/
我是一名优秀的程序员,十分优秀!