使用Java 1.6中的Swing库构建一个简单的GUI
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Java 1.6中的Swing库构建一个简单的GUI,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4104字,纯文字阅读大概需要6分钟。
内容图文
我正在尝试使用Swing库构建一个简单的GUI.我不明白为什么我的表正在删除之前添加到GUI的所有内容,然后才创建表.我假设它是addMainPanel中的某个命令,但我不确定是哪个.非常感谢您的建议.
package fuelConsumption;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LogView implements ActionListener {
private Log myLog;
private JFrame frame;
public LogView (String frameName) {
this.frame = new JFrame(frameName);
this.frame.setPreferredSize(new Dimension(500,500));
this.frame.getContentPane().setLayout(new BorderLayout());
this.addMainPanel(frame);
this.addTable(frame);
//addMenu(frame);
//addToolBar(frame);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.pack();
this.frame.setVisible(true);
}
private void addTable(JFrame frame2) {
String[] columnNames = {"date",
"station",
"fuel grade",
"fuel amount",
"fuel unit cost",
"fuel cost",
"trip distance"};
Object[][] data = {
{"Shell", 89, 40, 109.5, "bla", 100, 123}
};
JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
//this.frame.setContentPane(scrollPane);
frame2.getContentPane().add(scrollPane);
}
private void addMainPanel(JFrame frame2) {
// TODO Auto-generated method stub
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 5;
c.ipady = 50;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.5;
c.weighty = 0.5;
JLabel label = new JLabel("");
panel.add(label,c);
label = new JLabel("Info");
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(label,c);
label = new JLabel("Label");
c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 1;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(label,c);
label = new JLabel("Comments");
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 2;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(label,c);
JTextArea textArea = new JTextArea(4,30);
JScrollPane textScroll = new JScrollPane(textArea);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 4;
c.ipadx = 30;
c.ipady = 50;
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(textScroll,c);
JButton button = new JButton("Edit");
button.addActionListener(this);
button.setActionCommand("Edit");
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(button,c);
button = new JButton("Previous");
button.addActionListener(this);
button.setActionCommand("Previous");
c = new GridBagConstraints();
c.gridx = 2;
c.gridy = 3;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(button,c);
button = new JButton("Next");
button.addActionListener(this);
button.setActionCommand("Next");
c = new GridBagConstraints();
c.gridx = 3;
c.gridy = 3;
// c.weightx = 0.5;
// c.weighty = 0.5;
panel.add(button,c);
frame2.getContentPane().add(panel);
}
public static void main(String [] args){
new LogView("Fuel Consumption");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
解决方法:
您的GUI可能是:
如果你改变了
private void addTable(JFrame frame2){to private void addTable(){
和
frame2.getContentPane()添加(滚动面板). to frame.add(scrollPane,BorderLayout.CENTER);
至:
private void addMainPanel(JFrame frame2){to private void addMainPanel(){
和
.frame2.getContentPane()添加(面板); to frame.add(panel,BorderLayout.SOUTH);
因为在BorderLayout中只有一个JComponent可以放在其中一个区域中,因为没有BorderLayout常量的定义,所以JComponent将放在BorderLayout.CENTER区域中.
3)
然后你必须改变
this.addMainPanel(frame);
this.addTable(frame);
至
this.addMainPanel();
this.addTable();
内容总结
以上是互联网集市为您收集整理的使用Java 1.6中的Swing库构建一个简单的GUI全部内容,希望文章能够帮你解决使用Java 1.6中的Swing库构建一个简单的GUI所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。