我制作了一個按鈕來創建一個新視窗并被禁用。在我創建的新視窗中,我輸入另一個按鈕來關閉這個新打開的視窗并啟用第一個按鈕。我不知道該怎么做
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
button.setEnabled(false);
JButton button3 = new JButton();
button3.setBounds(1,1,50,25);
button3.addActionListener(this);
JFrame y = new JFrame();
y.setVisible(true);
y.setSize(240,240);
y.setLocationRelativeTo(null);
y.add(button3);
71. //here I want to use button3 to close window y and enable button 1 again, but how I
//make the button 3 do that?
if (e.getSource() == button3) {
button.setEnabled(true);
y.setVisible(false);
}
我無法閱讀 button3 的操作。如果你告訴我該怎么做,我真的會很感激。
uj5u.com熱心網友回復:
您的代碼和您的問題可以改進,包括:
- 任何“子”視窗都不應該是 JFrame,而是對話框視窗。JFrames 用于應用程式視窗,一次只能顯示其中一個(實際上,應該只創建一個),而當您的應用程式需要依賴子視窗或“子”視窗時,標準做法是使用對話框, Swing 實作為
JDialog
s。這些不會顯示在作業系統的任務欄中(而 JFrames 會顯示),并且單個應用程式應該只在作業系統的任務欄中顯示一個任務。 - Swing JDialogs 有兩種主要型別,模態的和非模態的。模態對話框,如 JOptionPane,阻止用戶與父視窗互動,直到對話框不再可見,而非模態對話框(如繪圖程式的編輯器工具箱)允許與父視窗互動。您的應用程式可能應該使用模式對話框。
- JDialogs 的創建方式與 JFrame 非常相似,只是您應該將對父視窗的參考傳遞給 JDialog 的建構式,并且您應該通過 ModalityType 引數將對話框是否為模態對話框傳遞給建構式。
- 如果子視窗是模態 JDialog,如果您想重新激活主父視窗,您所要做的就是關閉子視窗,在下面的示例中通過
window.dispose()
- 最好為程式的不同部分創建單獨的類,例如主視窗的類(或多個類)和任何子視窗的類。如果需要(如下所示),您可以在其建構式中將偵聽器添加到子視窗類。
其他更小問題:
- 最好避免空布局,
setBounds(...)
而是學習和使用 Swing 布局管理器來幫助您構建更易于構建的 GUI,在所有作業系統上看起來都很好,并且以后很容易更改 - 如果您使用模態JDialog,則無需禁用主視窗的按鈕,因為通過模態,用戶無法與主視窗互動,直到對話框不再可見。
- 通過讓您的類擴展 JFrame,您可能會把自己畫在一個角落里,迫使您創建和顯示 JFrame,而通常需要更大的靈活性。事實上,我敢冒昧地說,我創建的大部分 Swing GUI 代碼以及我所看到的都沒有擴展 JFrame,事實上,您很少會想要這樣做。更常見的是,您的 GUI 類將用于創建 JPanel,然后可以將其放入 JFrames 或 JDialogs 或 JTabbedPanes 中,或者在需要時通過 CardLayouts 進行交換。這將大大增加您的 GUI 編碼的靈活性。
例如(請參閱代碼中的注釋):
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import javax.swing.*;
@SuppressWarnings("serial")
// Avoid extending JFrame and instead extend JPanel. Even this is not necessary
public class W2Panel extends JPanel {
// I like using larger fonts if I want my buttons to be larger
private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
// variables for the 2 JButtons:
private JButton settingsButton = new JButton("Settings");
private JButton aboutButton = new JButton("About");
// variable for the sub window
private W2SettingsPanel w2SettingsPanel;
public W2Panel() {
// avoid setting sizes and instead set properties and let the GUI size itself
int gap = 120;
setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
settingsButton.setFont(BTN_FONT);
aboutButton.setFont(BTN_FONT);
// add an ActionListener to the JButton
settingsButton.addActionListener(e -> settingsAction());
// use layouts to set position of things
JPanel innerPanel = new JPanel(new GridLayout(3, 2));
innerPanel.add(settingsButton);
innerPanel.add(new JLabel(" "));
innerPanel.add(new JLabel(" "));
innerPanel.add(new JLabel(" "));
innerPanel.add(new JLabel(" "));
innerPanel.add(aboutButton);
// nest JPanels where needed
setLayout(new GridBagLayout());
add(innerPanel);
}
private void settingsAction() {
// if settings panel not yet created, then create it
if (w2SettingsPanel == null) {
// this gets a reference to the main JFrame:
Window window = SwingUtilities.getWindowAncestor(this);
// call the W2SettingsPanel constructor, passing in the JFrame's reference
w2SettingsPanel = new W2SettingsPanel(window);
}
w2SettingsPanel.display();
}
@SuppressWarnings("unused") // TODO: remove this
private void aboutActions() {
// TODO: create and display a modal dialog to display here as well
}
public static void main(String[] args) {
// create and display the main GUI in a Swing safe manner
SwingUtilities.invokeLater(() -> {
// create an instance of this class
W2Panel mainPanel = new W2Panel();
// create a JFrame, and add this instance into it
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack(); // let layouts do their thing and size the GUI
frame.setLocationRelativeTo(null); // center the GUI
frame.setVisible(true); // show it!
});
}
}
@SuppressWarnings("serial")
class W2SettingsPanel extends JPanel {
private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24);
private JDialog dialog;
private JButton backButton = new JButton("Back");
public W2SettingsPanel(Window window) {
int gap = 200;
setBorder(BorderFactory.createEmptyBorder(0, 0, gap, gap));
backButton.setFont(BTN_FONT);
backButton.addActionListener(e -> backAction());
add(backButton);
dialog = new JDialog(window, "Settings", ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.add(this);
dialog.pack();
dialog.setLocationRelativeTo(window);
}
public void backAction() {
// get the enclosing window (here a JDialog)
Window window = SwingUtilities.getWindowAncestor(this);
window.dispose(); // and dispose of it, make it disappear
}
public void display() {
dialog.setVisible(true);
}
}
uj5u.com熱心網友回復:
您必須在類中初始化 Jbutton button3,而不是在方法 actionPerformed() 中,并且必須將 if(e.getSource() == button3) 放入方法中,而不是在第一個 if-branch 的范圍內
uj5u.com熱心網友回復:
謝謝約翰很酷。我現在分享我的代碼,(因為我知道它沒用)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//go to line 71 and I am extremely sorry for including this long code
public class w extends JFrame implements ActionListener {
private static final long serialVersionUID = 1005007250764256307L;
public JButton button = new JButton();
public JButton button2;
JPanel panel;
boolean b = true;
w(){
button.setBounds(100,100,100,50);
button.addActionListener(this);
button.setBackground(Color.blue);
button.setText("Settings");
button.setFocusable(false);
button.setForeground(Color.green);
button.setEnabled(b);
button2 = new JButton();
button2.setBounds(200,200,100,50);
button2.addActionListener(this);
button2.setBackground(Color.red);
button2.setText("About");
button2.setFocusable(false);
button2.setForeground(Color.blue);
button2.setEnabled(true);
panel = new JPanel();
panel.setBounds(0, 0, 420, 420);
panel.setBackground(Color.GREEN);
this.setVisible(true);
this.setSize(420,420);
this.setLocationRelativeTo(null);
this.setTitle("Kukur er Maa");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.add(button2);
this.setLayout(null);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
button.setEnabled(false);
JFrame y = new JFrame();
JButton button3 = new JButton();
button3.setBounds(0,0,100,50);
button3.setBackground(Color.blue);
button3.setText("Back");
button3.setFocusable(false);
button3.setForeground(Color.green);
button3.setEnabled(true);
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button3) {
y.setVisible(false);
button.setEnabled(true);
}
}
});
y.setVisible(true);
y.setSize(420,420);
y.setLocationRelativeTo(null);
y.add(button3);
y.setLayout(null);
//here I want to use button3 to close window y and enable button 1 again, but how I
//make the button 3 do that?
if (e.getSource() == button3) {
button.setEnabled(true);
y.setVisible(false);
}
}
else if(e.getSource()== button2) {
JFrame ne = new JFrame("About");
ne.setVisible(true);
ne.setSize(240,240);
ne.setLocationRelativeTo(null);
button2.setEnabled(false);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/420484.html
標籤:
上一篇:在帶有換行符的TextArea上列印StringBuilder
下一篇:返回列表