英文:
I having problem with disposing the current window in swing
问题
以下是你提供的Java Swing应用程序的翻译:
这是一个Java Swing应用程序
我需要关闭当前窗口并导航到另一个窗口。但是在关闭当前窗口时它并没有关闭(退出),而是打开了一个新窗口。我已经尝试了几种方法,包括在不同的位置和方式初始化框架。有人可以帮助我吗?您可以在代码底部找到单击方法
我在每个窗口中都遇到了同样的问题。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddStudent extends JFrame {
// ...(省略其他内容,不进行翻译)
public AddStudent() {
// ...(省略其他内容,不进行翻译)
JButton btnNewButton = new JButton(img1);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StudentHome studentHome = new StudentHome();
frame.dispose();
studentHome.setVisible(true);
}
});
btnNewButton.setBounds(27, 11, 52, 48);
contentPane.add(btnNewButton);
JLabel lblHome = new JLabel("Home");
lblHome.setForeground(new Color(0, 0, 153));
lblHome.setBounds(37, 70, 46, 14);
contentPane.add(lblHome);
}
}
请注意,我只翻译了代码中的注释以外的内容。如果需要进一步的翻译或其他帮助,请随时提问。
英文:
This is a java swing application
I need to dispose the the current window and navigate to another. But i when disposing the current window it does not dispose (exit) but new window opens. I have tried several things including initializing the frame in various ways and places. can someone help me? You can find the onclick method at the bottom of the code
I having same problem with every window.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddStudent extends JFrame {
private JPanel contentPane;
private JTextField groupNo;
private JTextField programme;
private JTextField generatedGroupId;
private JTextField generatedSubGroupId;
private JTextField subGroup;
**static AddStudent frame;**
String sYear ="Y1",sSem="S2",sProgramme,sGroup,sSubGroup,sGroupId,sSubGroupId;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new AddStudent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
/**
*
*/
public AddStudent() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1000, 575);
contentPane = new JPanel();
contentPane.setForeground(new Color(153, 51, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblYear = new JLabel("Programme");
lblYear.setForeground(new Color(25, 25, 112));
lblYear.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblYear.setBounds(453, 196, 100, 25);
contentPane.add(lblYear);
JLabel label = new JLabel("Year");
label.setForeground(new Color(25, 25, 112));
label.setFont(new Font("Tahoma", Font.PLAIN, 15));
label.setBounds(453, 119, 46, 14);
contentPane.add(label);
JLabel label_1 = new JLabel("Semester");
label_1.setForeground(new Color(25, 25, 112));
label_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
label_1.setBounds(453, 158, 100, 14);
contentPane.add(label_1);
JLabel lblGroupId = new JLabel("Generate Group ID");
lblGroupId.setForeground(new Color(25, 25, 112));
lblGroupId.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblGroupId.setBounds(453, 287, 156, 25);
contentPane.add(lblGroupId);
JLabel lblGroupNo = new JLabel("Group No");
lblGroupNo.setForeground(new Color(25, 25, 112));
lblGroupNo.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblGroupNo.setBounds(453, 243, 100, 25);
contentPane.add(lblGroupNo);
JLabel lblSubGroupId = new JLabel("Sub Group No");
lblSubGroupId.setForeground(new Color(25, 25, 112));
lblSubGroupId.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblSubGroupId.setBounds(453, 335, 100, 25);
contentPane.add(lblSubGroupId);
JLabel lblGenerateSubGroup = new JLabel("Generate Sub Group ID");
lblGenerateSubGroup.setForeground(new Color(25, 25, 112));
lblGenerateSubGroup.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblGenerateSubGroup.setBounds(453, 382, 156, 25);
contentPane.add(lblGenerateSubGroup);
JLabel lblStudent = new JLabel("ADD STUDENT");
lblStudent.setForeground(new Color(0, 51, 51));
lblStudent.setFont(new Font("Tahoma", Font.PLAIN, 25));
lblStudent.setBounds(403, 11, 206, 59);
contentPane.add(lblStudent);
JLabel image = new JLabel("");
ImageIcon img = new ImageIcon(this.getClass().getResource("/class1.png"));
image.setIcon(img);
image.setBounds(103, 119, 300, 300);
contentPane.add(image);
groupNo = new JTextField();
groupNo.setBounds(651, 245, 199, 25);
contentPane.add(groupNo);
groupNo.setColumns(10);
programme = new JTextField();
programme.setText("");
programme.setColumns(10);
programme.setBounds(651, 198, 199, 25);
contentPane.add(programme);
String[] yearList = {"Y1","Y2","Y3","Y4"};
JComboBox year = new JComboBox(yearList);
year.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
sYear = (String)year.getSelectedItem();
}
});
year.setBounds(651, 111, 197, 25);
contentPane.add(year);
String[] semList = {"S1","S2"};
JComboBox semester = new JComboBox(semList);
semester.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
sSem = (String)semester.getSelectedItem();
}
});
semester.setBounds(651, 155, 197, 25);
contentPane.add(semester);
generatedGroupId = new JTextField();
generatedGroupId.setEditable(false);
generatedGroupId.setColumns(10);
generatedGroupId.setBounds(651, 291, 199, 25);
contentPane.add(generatedGroupId);
generatedSubGroupId = new JTextField();
generatedSubGroupId.setEditable(false);
generatedSubGroupId.setColumns(10);
generatedSubGroupId.setBounds(651, 382, 199, 25);
contentPane.add(generatedSubGroupId);
subGroup = new JTextField();
subGroup.setColumns(10);
subGroup.setBounds(651, 339, 199, 25);
contentPane.add(subGroup);
JButton btnGenerateGroupId = new JButton("Generate");
btnGenerateGroupId.setForeground(new Color(255, 255, 255));
btnGenerateGroupId.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
sGroup = groupNo.getText().toString();
sSubGroup = subGroup.getText().toString();
sProgramme = programme.getText().toString();
if(sGroup.isEmpty()) {
JOptionPane.showMessageDialog(null,
"Please Enter Group");
}else if(sProgramme.isEmpty()){
JOptionPane.showMessageDialog(null,
"Please Enter Programme");
}else {
sGroupId = sYear + "." + sSem + "."+ sProgramme + "."+ sGroup;
generatedGroupId.setText(sGroupId);
}
}
});
btnGenerateGroupId.setBackground(new Color(102, 51, 255));
btnGenerateGroupId.setBounds(869, 292, 89, 23);
contentPane.add(btnGenerateGroupId);
JButton btnGenerateSubGroupId = new JButton("Generate");
btnGenerateSubGroupId.setForeground(new Color(255, 255, 255));
btnGenerateSubGroupId.setBackground(new Color(102, 51, 255));
btnGenerateSubGroupId.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sGroup = groupNo.getText().toString();
sSubGroup = subGroup.getText().toString();
sProgramme = programme.getText().toString();
if(sGroup.isEmpty()) {
JOptionPane.showMessageDialog(null,
"Please Enter Group");
}else if(sSubGroup.isEmpty()) {
JOptionPane.showMessageDialog(null,
"Please Enter Sub Group");
}else if(sProgramme.isEmpty()){
JOptionPane.showMessageDialog(null,
"Please Enter Programme");
}else {
sSubGroupId = sYear + "." + sSem + "."+ sProgramme + "."+ sGroup + "." + sSubGroup;
generatedSubGroupId.setText(sSubGroupId);
}
}
});
btnGenerateSubGroupId.setBounds(869, 385, 89, 23);
contentPane.add(btnGenerateSubGroupId);
JButton btnAdd = new JButton("Add");
btnAdd.setForeground(new Color(255, 255, 255));
btnAdd.setBackground(new Color(102, 51, 255));
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sGroup = groupNo.getText().toString();
sSubGroup = subGroup.getText().toString();
sProgramme = programme.getText().toString();
if(sGroup.isEmpty()) {
JOptionPane.showMessageDialog(null,
"Please Enter Group");
}else if(sProgramme.isEmpty()){
JOptionPane.showMessageDialog(null,
"Please Enter Programme");
}else {
addDataConnection addDataConnection = new addDataConnection();
addDataConnection.insertStudent(sYear, sSem, sGroup, sSubGroup, sProgramme,sGroupId,sSubGroupId);
JOptionPane.showMessageDialog(null,
"Group Added !");
groupNo.setText("");
programme.setText("");
subGroup.setText("");
generatedSubGroupId.setText("");
groupNo.setText("");
generatedGroupId.setText("");
}
}
});
btnAdd.setBounds(723, 436, 72, 25);
contentPane.add(btnAdd);
ImageIcon img1 = new ImageIcon(this.getClass().getResource("/home.png"));
JButton btnNewButton = new JButton(img1);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StudentHome studentHome = new StudentHome();
frame.dispose();
studentHome.setVisible(true);
}
});
btnNewButton.setBounds(27, 11, 52, 48);
contentPane.add(btnNewButton);
JLabel lblHome = new JLabel("Home");
lblHome.setForeground(new Color(0, 0, 153));
lblHome.setBounds(37, 70, 46, 14);
contentPane.add(lblHome);
}
}
答案1
得分: 0
使用Windows类
有一个名为dispose
的方法。
JFrame,继承自Frame
,而Frame
又继承自Windows
。您的类继承自JFrame
,因此您可以直接访问dispose()
方法。
英文:
Use Windows class
has a method dispose
.
JFrame, extends Frame
, which in turn extends Windows
. Your class extends JFrame
, thus you have a direct access to dispose()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论