本文目錄一覽:
- 1、一個窗體,一個按鈕,最簡單的java代碼怎寫?
- 2、Java中怎麼在一個窗體點擊一個按鈕打開另一個窗體?
- 3、java怎樣放兩個按鈕在窗體的正中間
- 4、JAVA創建一個窗體添加兩個按鈕,為這兩個按鈕添加事件處理使窗體的下部分分面板背景顏色在黑白間轉換
- 5、JAVA如何用按鈕關閉窗體
一個窗體,一個按鈕,最簡單的java代碼怎寫?
public class Demo extends JFrame
{
JButton jb; //一個按鈕
public static void main(String []args){
new Demo();
}
public Demo()
{
this.setLayout(new FlowLayout());
jb=new JButton(“按扭”);
this.add(jb);
this.setSize(400,300);
this.setVisible(true);
this.setLocation(500, 200);
}
}
Java中怎麼在一個窗體點擊一個按鈕打開另一個窗體?
假如你的那個按鈕叫button,你要打開的那個窗體的類名叫Form2.
你在button的click事件裡面寫個
Form2 fm=new Form2();
fm.show();
就行了。。當然,你的Form2類,要設置Visible為True,同時設置大小位置。不然,你看不到窗體。
給你貼個代碼,你自己看吧
該代碼經過調試,驗證可行。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class formshow extends JFrame implements ActionListener{
JButton button;
public formshow(){
button=new JButton(“點擊我,出現新窗體”);
add(button);
button.addActionListener(this);
this.setLayout(new FlowLayout());
this.setBounds(520, 130, 200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
formshow fs=new formshow();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button){
form2 fm=new form2();
}
}
}
class form2 extends JFrame{
//第二個窗體;
JLabel jl;
static int n=1;
public form2(){
n++;
jl=new JLabel(“我是第”+n+”個窗體。”);
add(jl);
this.setTitle(“我是第”+n+”個窗體”);
//設置屬性。
this.setLayout(new FlowLayout());
this.setBounds(120+11*n, 230+10*n, 200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
java怎樣放兩個按鈕在窗體的正中間
JPanel 放入到BorderLayout.CENTER , 那麼會自動填充滿整個contentPane的中間, 而JPanel內部還是流式布局, 一行排滿 自動換到下一行,從上到下. 所以按鈕還是在最上面.
(把JPanel的背景色改成藍色,就可以清晰的看到JPanel填滿了窗口)
解決辦法:
方法一: [絕對布局],通過設置panel 為絕對布局,然後設置按鈕的寬高和位置
參考代碼
import javax.swing.*;
public class JFDemo1 extends JFrame{
public JFDemo1() {
JPanel panel=new JPanel();
panel.setLayout(null);//設置為空布局.或者叫絕對布局
JButton messageButton = new JButton(“OK”);
JButton closeButton = new JButton(“Cancel”);
panel.add(messageButton);
panel.add(closeButton);
//粗略的指定下位置,如果要精確的位置,需要進行計算
closeButton.setSize(80, 30);// 指定寬高
closeButton.setLocation(160, 75);//指定位置
messageButton.setBounds(80, 75, 60,30);// 同時指定寬高和位置
add(panel);//默認位置就是BorderLayout.CENTER
setTitle(“Demo”);// 標題
setSize(320, 230);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口點擊關閉時,退出程序
}
public static void main(String[] args) {
new JFDemo1().setVisible(true);
}
}
方法二:[盒布局]
import javax.swing.*;
public class JFDemo2 extends JFrame{
public JFDemo2() {
JPanel pane=new JPanel();
BoxLayout layout=new BoxLayout(pane, BoxLayout.X_AXIS);// 水平的盒布局
pane.setLayout(layout);
JButton messageButton = new JButton(“OK”);
JButton closeButton = new JButton(“Cancel”);
pane.add(Box.createGlue()); // 擠佔ok按鈕和窗口左側空間
pane.add(messageButton);
pane.add(Box.createHorizontalStrut(20));// 按鈕之間的水平距離
pane.add(closeButton);
pane.add(Box.createGlue()); // 擠佔cancel按鈕和窗口右側空間
add(pane);
setTitle(“Demo”);// 標題
setSize(320, 230);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口點擊關閉時,退出程序
}
public static void main(String[] args) {
new JFDemo2().setVisible(true);
}
}
總結: 推薦使用方法二,使用盒布局來實現.
一般不推薦使用絕對布局/空布局 來布局窗口, 因為不同的操作系統下顯示的效果不完全一致.
並且還需要寫大量的代碼來計算組件的大小和位置, 當窗口放大和縮小時 還需要重新計算位置
JAVA創建一個窗體添加兩個按鈕,為這兩個按鈕添加事件處理使窗體的下部分分面板背景顏色在黑白間轉換
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestWin extends JFrame implements ActionListener {
private JButton blackBtn=new JButton(“Black”);
private JButton whiteBtn=new JButton(“White”);
private JPanel pane=new JPanel();
public TestWin() {
this.blackBtn.addActionListener(this);
this.whiteBtn.addActionListener(this);
JPanel btnPane=new JPanel();
btnPane.add(this.blackBtn);
btnPane.add(this.whiteBtn);
this.add(btnPane,”North”);
this.add(pane,”Center”);
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==this.blackBtn) {
this.pane.setBackground(Color.BLACK);
}else if(source==this.whiteBtn) {
this.pane.setBackground(Color.WHITE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() – new TestWin());
}
}
JAVA如何用按鈕關閉窗體
很久沒有用過界面編程了,就當複習一下了,哈哈
如一樓所說的,給按鈕加一個監聽器ActionListener,寫一個實現方法
actionPerformed.此時當按鈕點擊時會調用actionPerformed方法,代碼如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Close extends JFrame implements ActionListener{
JButton close;
public Close(){
close = new JButton(“close”);//增加一個按鈕
add(close);
close.addActionListener(this);//給按鈕增加一個監聽器
setLayout(new FlowLayout());
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//捕捉到按鈕點擊時的事件處理方法
//按鈕點擊時一定會自動執行actionPerformed(ActionEvent e)方法
public void actionPerformed(ActionEvent e){
//關閉整個應用程序.如果只是是想關閉當前窗口,可以用
//dispose();
System.exit(0);
}
public static void main(String[] args){
new Close();
}
}
原創文章,作者:JROT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/148549.html