public class OpenFrame extends JFrame implements ActionListener {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private BudgetPlanner budgetPlanner;
private JLabel welcomeMessage;
private JPanel openpanel = new JPanel();
private JButton loadButton = new JButton();
private JButton resetButton = new JButton();
private JFrame openFrame;
private JLabel getWelcomeMessage;
public BudgetPlanner callBudgetPlanner() {
try {
budgetPlanner = new BudgetPlanner();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return budgetPlanner;
}
public OpenFrame() {
openFrame = new JFrame();
openFrame.setTitle("BudgetPlanner");
openFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
openFrame.setResizable(false);
openFrame.setLayout(null);
openFrame.setLocationRelativeTo(null);
openFrame.setSize(new Dimension(WIDTH, HEIGHT));
openFrame.getContentPane().setBackground(Color.GRAY);
openPanel();
openFrame.setContentPane(openpanel);
openFrame.setVisible(true);
}
public void openPanel() {
openpanel.setLayout(null);
welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
welcomeMessage.setVerticalTextPosition(TOP);
welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);
welcomeMessage.setBounds(50, 20, 500, 200);
welcomeMessage.setFont(new Font("Arial", Font.PLAIN, 17));
openpanel.setBounds(0, 0, 500, 500);
openpanel.add(welcomeMessage);
loadButton.setBounds(200, 170, 100, 50);
loadButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
loadButton.setText("Load Data");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoadCompletePanel loadCompletePanel = new LoadCompletePanel();
openpanel.add(loadCompletePanel);
}
});
loadButton.setFocusable(false);
resetButton.setBounds(200, 230, 100, 50);
resetButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
resetButton.setText("Reset Data");
resetButton.addActionListener(this);
resetButton.setFocusable(false);
openpanel.add(loadButton);
openpanel.add(resetButton);
}
以上是我的代碼,我有一個問題。我想通過單擊 loadButton 來呼叫一個新面板(在單獨的類中制作),但它似乎不起作用 - 即使我單擊該按鈕也沒有任何反應。
我應該如何解決這個問題?順便說一下,下面是新面板的代碼:
public class LoadCompletePanel extends JPanel implements ActionListener {
private BudgetPlanner budgetPlanner;
private JPanel loadCompletePanel;
private JLabel loadCompleteMessage;
private JButton addExpense;
private JButton addIncome;
private JButton viewMonthlyExpense;
private JButton viewMonthlyIncome;
public LoadCompletePanel() {
loadCompletePanel = new JPanel();
loadScreenPanel();
loadCompletePanel.add(loadCompleteMessage);
loadCompletePanel.setBounds(200, 170, 100, 50);
setVisible(true);
}
private void loadScreenPanel() {
loadCompletePanel.setLayout(null);
loadCompleteMessage = new JLabel("Loading Complete.");
loadCompleteMessage.setBounds(130, 50, 500, 200);
}
對不起,如果這是一個非常瑣碎和基本的問題。我仍然是 Java 的初學者。任何幫助,將不勝感激。
uj5u.com熱心網友回復:
我終于得到了你的代碼來做你期望它做的事情。我做了一些改變。如果您想查看我所做的更改,我建議您將您的代碼與下面的代碼進行比較。但是,我建議重寫您的應用程式。我已經這樣做了,重寫應用程式的代碼出現在我修改后的代碼版本之后。
這是您問題中代碼的修改版本。
(請注意,我在您的問題中找不到 classBudgetPlanner的代碼,但由于您問題中的代碼從不呼叫 method callBudgetPlanner,我只是洗掉了該方法。我還洗掉了未使用的匯入和未使用的變數。代碼后有更多注釋。)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class OpenFrame extends JFrame implements ActionListener {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private JLabel welcomeMessage;
private JPanel openpanel = new JPanel();
private JButton loadButton = new JButton();
private JButton resetButton = new JButton();
private JFrame openFrame;
public OpenFrame() {
openFrame = new JFrame();
openFrame.setTitle("BudgetPlanner");
openFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
openFrame.setResizable(false);
openFrame.setLayout(null);
openFrame.setLocationRelativeTo(null);
openFrame.setSize(new Dimension(WIDTH, HEIGHT));
openFrame.getContentPane().setBackground(Color.GRAY);
openPanel();
openFrame.setContentPane(openpanel);
openFrame.setVisible(true);
}
public void openPanel() {
openpanel.setLayout(null);
welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
welcomeMessage.setVerticalTextPosition(SwingConstants.TOP);
welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);
welcomeMessage.setBounds(50, 20, 500, 200);
welcomeMessage.setFont(new Font("Arial", Font.PLAIN, 17));
openpanel.setBounds(0, 0, 500, 500);
openpanel.add(welcomeMessage);
loadButton.setBounds(200, 170, 100, 50);
loadButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
loadButton.setText("Load Data");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoadCompletePanel loadCompletePanel = new LoadCompletePanel();
openpanel.removeAll();
openpanel.add(loadCompletePanel);
System.out.println("ADDED");
openpanel.revalidate();
openpanel.repaint();
}
});
loadButton.setFocusable(false);
resetButton.setBounds(200, 230, 100, 50);
resetButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
resetButton.setText("Reset Data");
resetButton.addActionListener(this);
resetButton.setFocusable(false);
openpanel.add(loadButton);
openpanel.add(resetButton);
}
public void actionPerformed(ActionEvent event) {
}
public static void main(final String[] args) {
EventQueue.invokeLater(() -> new OpenFrame());
}
}
class LoadCompletePanel extends JPanel {
private JPanel loadCompletePanel;
private JLabel loadCompleteMessage;
public LoadCompletePanel() {
super(null);
setBounds(0, 0, 300, 300);
loadCompletePanel = new JPanel();
loadScreenPanel();
loadCompletePanel.add(loadCompleteMessage);
loadCompletePanel.setBounds(200, 170, 100, 50);
add(loadCompletePanel);
setVisible(true);
}
private void loadScreenPanel() {
loadCompletePanel.setLayout(null);
loadCompleteMessage = new JLabel("Loading Complete.");
loadCompleteMessage.setBounds(1, 1, 50, 20);
loadCompletePanel.add(loadCompleteMessage);
}
}
最初的Swing示例代碼(來自上個世紀,大約 1998 年)總是將應用程式類擴展為JFrame或JPanel. 這不是必需的。
用戶單擊后,您似乎要替換openpanel為。由于您沒有使用
這是單擊 后應用程式的螢屏截圖loadButton。

您最好使用適當的
點擊后loadButton

請注意,我使用了以下 SO 問題的答案中的代碼,以便顯示loadCompleteMessage在其父容器的中心。
在 JPanel 上將 JLabel 居中
uj5u.com熱心網友回復:
請執行下列操作:
public void actionPerformed(ActionEvent e) {
LoadCompletePanel loadCompletePanel = new LoadCompletePanel();
openpanel.add(loadCompletePanel);
openPanel.repaint();
openPanel.revalidate();
}
此代碼將確保面板繪制新添加的組件。
uj5u.com熱心網友回復:
由于我不熟悉與 setBounds(...) 關聯的 setLayout(null) 并且很長時間沒有使用 Swing,因此我從未設法直接顯示 LoadCompletePanel 實體。無論如何,問題出在動作監聽器中,我用 LoadindCompleteLabel 標簽替換了 loadCompletePanel 面板。要使此標簽出現在動作偵聽器中,您必須在受影響的容器上呼叫 repaint()。
public void openPanel() {
openpanel.setLayout(null);
welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
welcomeMessage.setVerticalTextPosition(SwingConstants.TOP);
welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);
welcomeMessage.setBounds(50, 20, 500, 200);
welcomeMessage.setFont(new Font("Arial", Font.PLAIN, 17));
openpanel.setBounds(0, 0, 500, 500);
openpanel.add(welcomeMessage);
openpanel.add(loadCompletePanel);
JLabel LoadindCompleteLabel = new JLabel("Loading Complete.");
LoadindCompleteLabel.setBounds(130, 50, 500, 200);
loadButton.setBounds(200, 170, 100, 50);
loadButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
loadButton.setText("Load Data");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openpanel.add(LoadindCompleteLabel);
openpanel.repaint();
}
});
loadButton.setFocusable(false);
resetButton.setBounds(200, 230, 100, 50);
resetButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
resetButton.setText("Reset Data");
resetButton.addActionListener(this);
resetButton.setFocusable(false);
openpanel.add(loadButton);
openpanel.add(resetButton);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/445633.html
下一篇:JPanel中未顯示按鈕
