日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java frame清除控件_java – 清除JFrame的组件并添加新组件

發(fā)布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java frame清除控件_java – 清除JFrame的组件并添加新组件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我有一個JFrame,它有一些選項.當按下OK按鈕時,我想要相同的JFrame清除內(nèi)容并添加新內(nèi)容.我試過了,但問題是新的JFrame被彈出.我究竟做錯了什么?

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.*;

public class GuiFrame extends JFrame {

final JFrame f = new JFrame("Test");

public void Starter(){

ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");

f.setIconImage(img.getImage());

ButtonGroup group = new ButtonGroup();

final JRadioButton Acess = new JRadioButton("Acess");

final JRadioButton Chat = new JRadioButton("Chat");

group.add(Acess);

group.add(Chat);

f.setSize(400,100);

f.setLocationRelativeTo(null);

JButton OptionOk = new JButton("OK");

Label option = new Label("Choose a Option");

final Container content = f.getContentPane();

content.setBackground(Color.white);

content.setLayout(new FlowLayout());

content.add(option);

content.add(Acess);

content.add(Chat);

content.add(OptionOk);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

OptionOk.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

new GuiFrame().Initiate();

} catch (UnknownHostException ex) {

Logger.getLogger(GuiFrame.class.getName()).log(Level.SEVERE, null, ex);

}

}

});

}

public void Initiate() throws UnknownHostException {

f.removeAll();

ButtonGroup group = new ButtonGroup();

final JRadioButton ButtonServer = new JRadioButton("Server");

final JRadioButton ButtonClient = new JRadioButton("Client");

group.add(ButtonServer);

group.add(ButtonClient);

f.setSize(400, 100);

f.setLocationRelativeTo(null);

InetAddress thisIp = InetAddress.getLocalHost();

ImageIcon img = new ImageIcon("C:\\Users\\neal\\Desktop\\no.png");

f.setIconImage(img.getImage());

Label lip = new Label("Your IP is : " thisIp.getHostAddress());

Label setup = new Label("Setup as ");

JButton ButtonOk = new JButton("OK");

final Container content = f.getContentPane();

content.setBackground(Color.white);

content.setLayout(new FlowLayout());

content.add(lip);

content.add(setup);

content.add(ButtonServer);

content.add(ButtonClient);

content.add(ButtonOk);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) throws UnknownHostException {

GuiFrame gf = new GuiFrame();

gf.Starter();

}

}

解決方法:

解決方案很簡單:使用CardLayout,讓這個布局管理器為您完成所有繁重的工作.有關(guān)如何執(zhí)行此操作的更多詳細信息,請參閱教程:How to use CardLayout

至于你的代碼,請注意你實際上在啟動時創(chuàng)建了2個JFrame,如果推送了JButton則還有兩個:

GuiFrame類本身擴展了JFrame,但它似乎是一個你永遠不會使用的JFrame,因此被浪費了,但它在程序啟動時以及每當創(chuàng)建GuiFrame實例時創(chuàng)建,例如按下按鈕時.然后在這個類的內(nèi)部創(chuàng)建另一個JFrame f,一個在程序啟動時再次按下按鈕,我不認為這是你想要做的.

因此,更改代碼以使類不擴展JFrame,并且不要在按鈕的ActionListener中創(chuàng)建類的新實例.而是使用CardLayout交換視圖.

例如:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class GuiFrame {

private static final String FIRST_PANEL = "First Panel";

private static final String SECOND_PANEL = "Second Panel";

final JFrame f = new JFrame("Test");

private CardLayout cardLayout = new CardLayout();

private JPanel content;

public void Starter() {

f.setSize(400, 100);

f.setLocationRelativeTo(null);

JButton OptionOk = new JButton("OK");

Label option = new Label("Choose a Option");

content = (JPanel) f.getContentPane();

content.setLayout(cardLayout);

JPanel firstPanel = new JPanel();

firstPanel.setBackground(Color.white);

firstPanel.setLayout(new FlowLayout());

firstPanel.add(option);

firstPanel.add(OptionOk);

content.add(firstPanel, FIRST_PANEL);

content.add(createSecondPanel(), SECOND_PANEL);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

OptionOk.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

cardLayout.show(content, SECOND_PANEL);

}

});

}

private JPanel createSecondPanel() {

JPanel secondPanel = new JPanel();

secondPanel.add(new JButton(new AbstractAction("Go Back") {

public void actionPerformed(ActionEvent e) {

cardLayout.show(content, FIRST_PANEL);

}

}));

return secondPanel;

}

public static void main(String[] args) {

GuiFrame gf = new GuiFrame();

gf.Starter();

}

}

來源:https://www.icode9.com/content-1-463801.html

總結(jié)

以上是生活随笔為你收集整理的java frame清除控件_java – 清除JFrame的组件并添加新组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。