導航:首頁 > 編程語言 > java按鈕連接另一個

java按鈕連接另一個

發布時間:2025-05-02 00:14:39

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里,怎麼利用一個單獨的類生成的窗體里的按鈕控制另一個主窗體的背景顏色。

寫過一個類似的java程序,效果圖,如下, 思路分享到後面.

實現的方法很多專.代碼比較亂,所以提供幾種屬思路:

  1. 可以採用MVC結構, 使用Controller 來控制 窗口 的顏色進行改變

  2. 也可以使用帶有返回值的對話框(彈出框),用返回值來控制窗口的背景顏色

  3. 也可以把主窗口當成參數傳入 新的窗口裡, 然後在新的窗口裡點擊按鈕時,改變主窗口的背景色

  4. 也可以用一個配置文件作為橋梁, 當在新的窗口裡設置背景顏色,等參數時 修改配置文件.然後主窗口讀取配置文件, 來修改背景色(可能需要 重啟才能改變外觀)

  5. 使用JavaFX來替換Swing框架, 因為JavaFX更換背景,變更外觀很方便,就是替換CSS文件即可

⑶ Java單擊確定按鈕跳轉到另一個界面的代碼。調到另一個類的界面

public
void
actionPerformed(ActionEvent
e)
{
if(e.getSource()
==
button)
//或者e.getActionCommand().equals("確定')
{
Login
window
=
new
Login();
window.frame.setVisible(true);
}
}
這樣就可以了。但是要在Login類中定義一個全局變數frame,即:回private
JFrame
frame,並且記得初始化答,frame
=new
JFrame();

⑷ java中如何從一個窗口通過點擊按鈕進入另外一個窗口

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame1 extends JFrame {

private static final long serialVersionUID = 1L;
public static void main(String args[]) {
try {
Frame1 frame = new Frame1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Frame1() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton btn = new JButton();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Frame2();
}
});
btn.setText("OpenFrame2");
btn.setBounds(92, 107, 106, 26);
getContentPane().add(btn);
}
}
=============================================
import javax.swing.JFrame;

public class Frame2 extends JFrame {

private static final long serialVersionUID = 1L;

public Frame2() {
super();
setTitle("Frame2");
setBounds(100, 100, 215, 99);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

⑸ java中如何點擊按鈕跳轉到網頁

你說的是SWING編程吧 這個我不太熟悉 不過SWING中有十三個介面 有個介面就是按扭的介面 你實現了這個介面之後 實現其相應的方法 給加個超鏈就能鏈到你想要的網頁上去了
去看下java API 一看就查出來了

⑹ 在Java中怎麼在文本域輸入特定文字,實現單擊一哈按鈕,在另一個文本框內顯示特定的內容

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;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class frame extends JFrame
{
JLabel lable1;
JTextArea text2;
JTextField text1;
JButton button;
/**
*
*/
private static final long serialVersionUID = 1L;

public void shapeSearch ( boolean b )
{
this.setTitle ("圖形屬性查詢器");
this.setSize (400, 250);
this.setResizable (b);
this.setVisible (true);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}

public void imputDemo ()
{
JLabel lable1 = new JLabel ("圖形名稱:");
text2 = new JTextArea ();
text2.setText (null);
text2.setLineWrap (true);
text2.setBounds (20, 20, 300, 200);
text1 = new JTextField (10);
JButton button = new JButton ("查詢");
JScrollPane scroll = new JScrollPane (text2);
FlowLayout seeText = new FlowLayout (FlowLayout.CENTER);
seeText.setHgap (20);
seeText.setVgap (60);
this.setLayout (seeText);
this.add (lable1);
this.add (text1);
this.add (button);
this.add (scroll);
ButtonHandle handle = new ButtonHandle ();
button.addActionListener (handle);
DocumentWriter dm = new DocumentWriter ();
Document doc = text1.getDocument ();
doc.addDocumentListener (dm);
this.setVisible (true);
}

class ButtonHandle implements ActionListener
{
public void actionPerformed ( ActionEvent event )
{
if (event.getActionCommand ().equals ("查詢"))
{
if ("圓形".equals (text1.getText ()))
{
text2.setText ("該圓形的半徑是3\n" + "周長是18.84\n" + "面積是28.26");
}
else if ("矩形".equals (text1.getText ()))
{
text2.setText ("該矩形的長是5,寬是4\n" + "周長是18\n" + "面積是20");
text2.setVisible (true);
}
else if ("三角形".equals (text1.getText ()))
{
text2.setText ("該三角形的底邊是3,高是4,斜邊是5\n" + "周長是12\n" + "面積是6");
text2.setVisible (true);
}
}
}
}

class DocumentWriter implements DocumentListener
{
public void changedUpdate ( DocumentEvent e )
{}

public void insertUpdate ( DocumentEvent e )
{}

public void removeUpdate ( DocumentEvent e )
{}
}

public static void main ( String[] args ) throws Exception
{
frame littleFrame = new frame ();
littleFrame.shapeSearch (false);
littleFrame.imputDemo ();
}
}

⑺ 怎麼用JAVA 界面設計中實現點擊按鈕後增加一個按鈕並保存設置 並且如何使增的按鈕放在點擊的按鈕前面

在添加控抄件的時候,可以設置襲控制項的位置add(Component comp,int index)
你也可以得到添加的個數getComponentCount()
你這樣就可以隨意的添加到哪一個控制項前面了,
如果你想保存你的這些設置,
只保存一次,下次程序啟動的時候重新設置的話,你可以使用變數去記錄這些位置
如果下次程序啟動的時候要記錄上一次的最後保存信息,你可以把這些設置寫到xml中,或者用一個類去記錄這些熟悉,用序列化的方式保存到文件,啟動的時候去讀取這個xml或者反序列化文件,得到配置的信息

⑻ java實現簡單登錄界面,就是按個按鈕就能從一個JFRAME跳轉到另一個,基本就是按登錄就能跳轉

java實現的簡單登錄頁面,從一個按鈕點擊後跳轉的頁面的jframe寫法:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jb{
public static void main(String args[]){
JFrame f=new JFrame("點我跳轉");
Container contentPane=f.getContentPane();
contentPane.setLayout(new GridLayout(1,2));
Icon icon=new ImageIcon("b.jpg");
JLabel label2=new JLabel("a",icon,JLabel.CENTER);
label2.setHorizontalTextPosition(JLabel.CENTER);
contentPane.setLayout(new FlowLayout( FlowLayout.CENTER,10,10));
JButton bb=new JButton("圖片");
bb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFrame bf=new JFrame("新窗體");
Icon icon=new ImageIcon("enter.jpg");
JLabel label2=new JLabel(icon);
bf.getContentPane().add(label2);
bf.setSize(300,360);
bf.show();
}});
contentPane.add(label2);
contentPane.add(bb);
f.pack();
f.show();
}}

閱讀全文

與java按鈕連接另一個相關的資料

熱點內容
編程英雄中哪個最強 瀏覽:775
單反ps教程 瀏覽:255
海貝音樂蘋果版找不到文件夾 瀏覽:267
榮耀6窗口小工具 瀏覽:233
電腦雙擊文件夾顯示找不到 瀏覽:83
電腦大型文件傳輸軟體 瀏覽:407
ps重命名文件標題名稱 瀏覽:83
ug80表達式教程 瀏覽:26
騰訊視頻下載的文件保存位置 瀏覽:467
電腦重裝後找不到2k文件 瀏覽:524
有個文件消失了搜索也找不到 瀏覽:186
天下3升級75世界套 瀏覽:856
鵬博士數據港屬於哪個板塊 瀏覽:635
xp去除盜版工具 瀏覽:157
角度雲計算和大數據哪個好 瀏覽:871
js製作35選7 瀏覽:621
雷騰滑鼠找不到文件 瀏覽:357
興趣愛好都有哪些app 瀏覽:589
讓兩個大腦文件共享的軟體 瀏覽:806
機械手編程里的組合是什麼意思 瀏覽:605

友情鏈接