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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java frame button_java – 从Button打开一个新的JFrame

發布時間:2025/3/13 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java frame button_java – 从Button打开一个新的JFrame 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我想通過單擊按鈕(btnAdd)打開一個新的JFrame;我試圖創建一個actionlistener,但我沒有運氣;代碼運行但單擊按鈕時沒有任何反應.有問題的方法是以下代碼中的最后兩個.任何幫助深表感謝!

package AdvancedWeatherApp;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BoxLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

import javax.swing.event.ListSelectionListener;

import weatherforecast.FetchWeatherForecast;

public class MainFrame extends JFrame implements ListSelectionListener {

private boolean initialized = false;

private Actions actions = new Actions();

private javax.swing.JScrollPane jspFavouritesList = new javax.swing.JScrollPane();

private javax.swing.DefaultListModel listModel = new javax.swing.DefaultListModel();

private javax.swing.JList favouritesList = new javax.swing.JList(

listModel);

private javax.swing.JLabel lblAcknowledgement = new javax.swing.JLabel();

private javax.swing.JLabel lblTitle = new javax.swing.JLabel();

private javax.swing.JButton btnAdd = new javax.swing.JButton();

private javax.swing.JButton btnRemove = new javax.swing.JButton();

public void initialize() {

initializeGui();

initializeEvents();

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

/**

*

*/

private void initializeGui() {

if (initialized)

return;

initialized = true;

this.setSize(500, 400);

Dimension windowSize = this.getSize();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

this.setLocation(screenSize.width / 2 - windowSize.width / 2,

screenSize.height / 2 - windowSize.height / 2);

Container pane = this.getContentPane();

pane.setLayout(new BorderLayout());

setLayout(new BorderLayout());

setTitle("Favourite Weather Locations");

JPanel jpSouth = new JPanel();

jpSouth.setLayout(new FlowLayout());

JPanel jpNorth = new JPanel();

jpNorth.setLayout(new FlowLayout());

JPanel jpCenter = new JPanel();

jpCenter.setLayout(new BoxLayout(jpCenter, BoxLayout.PAGE_AXIS));

JPanel jpEast = new JPanel();

JPanel jpWest = new JPanel();

getContentPane().setBackground(Color.WHITE);

jpEast.setBackground(Color.WHITE);

jpWest.setBackground(Color.WHITE);

jpCenter.setBackground(Color.WHITE);

getContentPane().add(jspFavouritesList);

jpCenter.add(jspFavouritesList);

jspFavouritesList.setViewportView(favouritesList);

favouritesList

.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

favouritesList.addListSelectionListener(this);

jpCenter.add(btnAdd);

jpCenter.add(btnRemove);

jpCenter.setAlignmentY(CENTER_ALIGNMENT);

btnAdd.setText("Add Location");

btnAdd.setAlignmentX(Component.CENTER_ALIGNMENT);

btnAdd.setFont(new Font("Calibri", Font.PLAIN, 18));

jpCenter.add(btnRemove);

btnRemove.setText("Remove Location");

btnRemove.setAlignmentX(Component.CENTER_ALIGNMENT);

btnRemove.setFont(new Font("Calibri", Font.PLAIN, 18));

getContentPane().add(jpEast, BorderLayout.EAST);

getContentPane().add(jpWest, BorderLayout.WEST);

getContentPane().add(jpSouth);

jpSouth.add(lblAcknowledgement);

add(lblAcknowledgement, BorderLayout.SOUTH);

lblAcknowledgement.setText(FetchWeatherForecast.getAcknowledgement());

lblAcknowledgement.setHorizontalAlignment(SwingConstants.CENTER);

lblAcknowledgement.setFont(new Font("Tahoma", Font.ITALIC, 12));

getContentPane().add(jpNorth);

jpNorth.add(lblTitle);

add(lblTitle, BorderLayout.NORTH);

lblTitle.setText("Your Favourite Locations");

lblTitle.setHorizontalAlignment(SwingConstants.CENTER);

lblTitle.setFont(new Font("Calibri", Font.PLAIN, 32));

lblTitle.setForeground(Color.DARK_GRAY);

getContentPane().add(jpCenter);

}

private void initializeEvents() {

// TODO: Add action listeners, etc

}

public class Actions implements ActionListener {

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

command = command == null ? "" : command;

// TODO: add if...if else... for action commands

}

}

public void dispose() {

// TODO: Save settings

// super.dispose();

System.exit(0);

}

public void setVisible(boolean b) {

initialize();

super.setVisible(b);

}

public static void main(String[] args) {

new MainFrame().setVisible(true);

}

public void actionPerformed(ActionEvent evt){

if (evt.getSource() == btnAdd) {

showNewFrame();

//OPEN THE SEARCH WINDOW

}

}

private void showNewFrame() {

JFrame frame = new JFrame("Search Window" );

frame.setSize( 500,120 );

frame.setLocationRelativeTo( null );

frame.setVisible( true );

}

}

總結

以上是生活随笔為你收集整理的java frame button_java – 从Button打开一个新的JFrame的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。