這是我需要完成的第二個作業。以下是分配信息。展開和收縮按鈕點擊
用兩個按鈕創建一個框架,稱爲展開和收縮。當單擊展開按鈕時,框架擴展10%。單擊縮小按鈕時,框架縮小10%。通過使用setSize()在actionPerformed()方法中執行此操作。使用兩個int類型的實例變量來跟蹤幀的當前大小。當你增加或減少10%時,你將不得不使用整數算術或使用類型轉換。
我已經有框架和按鈕設置。當然,從網上調查來看,我只是無法正確理解。防爆。每次單擊收縮按鈕時,內側框架都會收縮,同樣,展開的內容也會相同(在框架內展開)。請看看代碼。順便說一句,請給我一個更好的方法來創建框架和代碼少的按鈕。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Expandshrink extends JFrame implements ActionListener
{
JButton expand;
JButton shrink;
Double w = 300.0, l = 200.0;
Expandshrink(String title)
{
expand = new JButton("Expand");
shrink = new JButton("Shrink");
expand.setActionCommand("expand");
shrink.setActionCommand("shrink");
expand.addActionListener(this);
shrink.addActionListener(this);
setLayout(new FlowLayout());
add(expand);
add(shrink);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent evt)
{
try
{
if (evt.getActionCommand().equals("expand"))
{
w = w*1.1;
l = l*1.1;
}
else if (evt.getActionCommand().equals("shrink"))
{
w = w*0.9;
l = l*0.9;
}
getContentPane().setSize(w.intValue(),l.intValue());
}
catch (Exception ex)
{
}
}
public static void main (String[] args)
{
Expandshrink frm = new Expandshrink("Expand & Shrink");
frm.setSize(300, 200);
frm.setVisible(true);
}
}
使用代碼塊一致性和邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 – 2013-03-19 04:09:29
是的,我同意下面的MadProg。總的來說,'getContentPane().setSize(w.intValue(),l.intValue());'這行對改變按鈕大小沒有影響。這是越來越整個事情,n不是按鈕 – Coffee 2013-03-19 04:18:21
小費另見本 - http://stackoverflow.com/questions/218155/how-do-i-change-jpanel-inside-a-jframe-on-the-fly – Coffee 2013-03-19 04:18:55