0
當我點擊搜索按鈕時,我的代碼重複了主屏幕的創建。搜索事件應該只打開一個帶有文本框的新框架來鍵入查詢。另一個框架的創建會導致這種重複或者這是一種錯誤?我嘗試使用mainFrame上傳searchPane的日期(代碼中的p1),但是當我這樣做時,這解決了我的窗口問題,但Search Windows上的defaultCloseOperation()會導致整個程序關閉 - 我怎麼能解決這個問題?主屏幕上的JFrame重複
在此先感謝
SearchScreen
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchScreen extends MainScreen{
JButton btsearch;
JLabel lbsearch;
protected JTextField txtsearch;
JPanel p1;
protected JFrame searchFrame = new JFrame();
public SearchScreen(){
//Button Creation
btsearch= new JButton("Search");
//Label Creation
lbsearch= new JLabel("Type Keywords in english to be searched below:");
//TextBox
txtsearch= new JTextField();
//Pane Creation
p1=new JPanel();
p1.setBackground(Color.gray);
//Pane Components
p1.add(lbsearch);
p1.add(txtsearch);
p1.add(btsearch);
//JFrame Layout Setup
p1.setLayout(new GridLayout(3,3));
btsearch.setEnabled(true);
//Adding JPaneel
searchFrame.add(p1);
//JFrame Setup
searchFrame.setTitle("SHST");
searchFrame.setSize(400, 400);
searchFrame.setVisible(true);
searchFrame.setDefaultCloseOperation(1);
}
}
MainScreen
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class MainScreen implements ActionListener {
JMenuBar bar;
JMenu file, register;
JMenuItem close, search;
JPanel entrance = new JPanel();
JFrame mainFrame = new JFrame();
public MainScreen()
{
bar= new JMenuBar();
file= new JMenu("File");
register= new JMenu("Search");
close= new JMenuItem("Close");
close.addActionListener(this);
search= new JMenuItem("Request Query");
search.addActionListener(this);
//mainFrame Setup
bar.add(file);
bar.add(register);
file.add(close);
register.add(search);
mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH);
mainFrame.setTitle("SHST");
mainFrame.setJMenuBar(bar);
mainFrame.setDefaultCloseOperation(0);
mainFrame.setVisible(true);
WindowListener J=new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
};
mainFrame.addWindowListener(J);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
if(e.getSource()==search){
SearchScreen s= new SearchScreen();
}
public static void main (String[] args){
MainScreen m= new MainScreen();
}
}
流 - 很快;它現在有效,但我認爲你的意思是「使用hertiance」否?因爲我刪除了來自searchScreen的遺傳,並且現在像Charme一樣工作; D –
@VictorOliveira我不是英語母語的人,可能是我創建了不存在的詞xD ..我的意思是使用具體的繼承(具體類)是危險和罕見的是necesesary – nachokk
@nachkokk抱歉的延遲,第一次在投票中,我應該等待5分鐘更多的接受,我不得不離開電腦 - 但真的很感謝花花公子hehehe –