这是我运行Java应用程序时显示的内容. (请参见我的按钮上的文本,文本框中的文本为java)
我在Windows 7上使用Eclipse Luna.
public class DownloadManager {
private JFrame frame;
private JTable table;
private JTextField txtUrl;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DownloadManager window = new DownloadManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DownloadManager() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 752, 514);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
table.setBounds(47, 190, 629, 250);
frame.getContentPane().add(table);
txtUrl = new JTextField();
txtUrl.setBounds(47, 84, 391, 34);
frame.getContentPane().add(txtUrl);
txtUrl.setColumns(10);
JButton btnDownload = new JButton("Download");
btnDownload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnDownload.setBounds(534, 78, 99, 47);
frame.getContentPane().add(btnDownload);
}
}
解决方法:
使用layout manager减轻上面显示的伪像. nests下面的示例中一个JPanel另一个.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
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.JTextField;
public class DownloadManager {
private JFrame frame;
private JTable table;
private JTextField txtUrl;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DownloadManager window = new DownloadManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DownloadManager() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = new JTable() {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(320, 240);
}
};
frame.add(new JScrollPane(table), BorderLayout.CENTER);
txtUrl = new JTextField(12);
txtUrl.setColumns(10);
JButton btnDownload = new JButton("Download");
btnDownload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
}
});
JPanel panel = new JPanel();
panel.add(txtUrl);
panel.add(btnDownload);
frame.add(panel, BorderLayout.NORTH);
frame.pack();
}
}
标签:layout,swing,jpanel,jframe,java
来源: https://codeday.me/bug/20191029/1957615.html