【发布时间】:2019-03-07 13:59:29
【问题描述】:
我正在尝试学习一些有关 Swing 的 GUI 界面以及如何正确构建 GUI 程序的知识,当然至少以最有效的方式取决于项目。
在此示例中,我尝试创建一个执行以下操作的简单程序。
我有一个由 2 个按钮组成的菜单,每个按钮在被按下时都会触发相应的 ActionListener,并会创建一个新类的实例。
每个新类(其中 2 个的名称是:Journal、Seminar)都有自己的 GUI 代码,由文本字段、按钮和其他摆动组件。
- 起初,我在 Journal's 和 Seminar's Logic 类中有这 2 个 GUI 类。但据我了解,这不是一个好习惯,看来我们需要让程序组件之间的连接不那么紧密。
- 所以现在我尝试将每个 GUI 方法移动到另一个类并在每个类的构造函数中调用它们。
注意:程序的最终目的是将对象序列化到文件中,因此暂时忽略一些与其对应的代码。
该代码目前无法正常工作。
** 问题:**
我在整个程序中采用的方法“正确”吗?还是我应该找到其他方法?
我应该学习哪些知识才能了解如何高效地构建此类应用程序?
我怎样才能使当前工作?
这是我在这里的第一篇文章,所以我尽我所能以最好的方式提出这个问题,如果您需要更多信息,请告诉我。谢谢!*
非常感谢任何帮助。
菜单类
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class Menu {
JFrame menu_frame;
JButton ergasia_periodiko_btn = new JButton();
JButton ergasia_sinedrio_btn = new JButton();
Menu() {
this.menu_frame = new JFrame();
this.menu_frame.setSize(400,100);
this.menu_frame.setTitle("Ereunitikos Katalogos");
this.menu_frame.add(ergasia_periodiko_btn);
this.menu_frame.add(ergasia_sinedrio_btn);
this.menu_frame.setLayout(new FlowLayout());
this.ergasia_periodiko_btn.setText("Periodika");
this.ergasia_sinedrio_btn.setText("Sinedria");
this.ergasia_periodiko_btn.addActionListener(e -> new Journal()); //Action Listener to Joyrnal
this.ergasia_sinedrio_btn.addActionListener(e -> {
try {
new Seminar();
} catch (IOException e1) {
e1.printStackTrace();
}
}); //Action Listener to Seminar
this.menu_frame.setLocationRelativeTo(null);
this.menu_frame.setVisible(true);
}
public static void main(String[] args)
{ new Menu(); }
}
期刊类
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Journal implements Serializable
{
Journal_GUI gui; //**Creating instance of journal gui inside here so i can run the gui code in the constructor**
String column_title;
List writers = new ArrayList(2);
String mag_title;
String numberOfPages;
String released_date;
String volume;
String exact_page;
Journal(){}
public void fillVars()
{
this.column_title = gui.column_title_tf.getText();
this.writers = Collections.singletonList(gui.writers_tf.getText());
this.mag_title = gui.mag_title_tf.getText();
this.numberOfPages = gui.numberOfPages_tf.getText();
this.released_date = gui.released_date_tf.getText();
this.volume = gui.volume_tf.getText();
this.exact_page = gui.exact_page_tf.getText();
}
public void insertP() {
fillVars();
// try {
// My_Serialization.serialization("fileToSavePeriodiko.txt", this.toString());
// }// catch (IOException e) {
// e.printStackTrace();
//}
}
public void searchP_byTitle(){}
public void searchP_byName(){}
@Override
public String toString() {
String value = "\n Periodiko column title : " + column_title + "\n writers : " + writers+ "\n Titlos magazine : " + mag_title
+ "\n Number of Pages : " + numberOfPages + "\n Released Date : " + released_date + "\n Volume : " + volume + "\n Exact Page : " + exact_page +"\n";
return value;
}
}
期刊桂课堂
import javax.swing.*;
public class Journal_GUI extends Journal{
JFrame periodikoFrame;
JTextField column_title_tf;
JTextField writers_tf;
JTextField mag_title_tf;
JTextField numberOfPages_tf;
JTextField released_date_tf;
JTextField volume_tf;
JTextField exact_page_tf;
public Journal_GUI(){
initComponenets();
}
public void initComponenets() {
periodikoFrame = new JFrame("PERIODIKA");
periodikoFrame.setSize(500, 500);
JPanel panel = new JPanel();
BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxlayout);
column_title_tf = new JTextField("Column Title");
writers_tf = new JTextField("Writers");
mag_title_tf = new JTextField("Magazine's Title");
numberOfPages_tf = new JTextField("Number of Pages");
released_date_tf = new JTextField("Date of Release");
volume_tf = new JTextField("Volume");
exact_page_tf = new JTextField("Exact Page");
JButton search_mag_btn_byName = new JButton("Search By name");
JButton search_mag_btn_byTitle = new JButton("Search By Title");
JButton insert_mag_btn = new JButton("Insert article");
search_mag_btn_byName.addActionListener(e -> searchP_byName());
search_mag_btn_byTitle.addActionListener(e -> searchP_byTitle());
insert_mag_btn.addActionListener(e -> insertP());
panel.add(column_title_tf);
panel.add(writers_tf);
panel.add(mag_title_tf);
panel.add(numberOfPages_tf);
panel.add(released_date_tf);
panel.add(volume_tf);
panel.add(exact_page_tf);
panel.add(search_mag_btn_byName);
panel.add(search_mag_btn_byTitle);
panel.add(insert_mag_btn);
periodikoFrame.setLocationRelativeTo(null);
periodikoFrame.add(panel);
periodikoFrame.setVisible(true);
}
}
【问题讨论】:
-
@Adam 感谢编辑伙伴。
-
Swing 而不是 JavaFX 的任何原因?后者旨在替代前者。
-
没有真正考虑过,但是是的,你是对的。