0
所以這是我當前項目的註釋中給出的示例。通常我會運行示例代碼並使用它來查看一切是如何工作的。但是我不確定如何正確調用示例代碼中的函數。使用eclipse的Java中的GUI應用程序
這東西是給我:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JPanel implements ActionListener
{
JTextField tfInfo;
JLabel lblColor, lblShapes;
JCheckBox cbRed, cbBlue;
ButtonGroup shapes;
JRadioButton rbCircle, rbSquare;
JButton btnSubmit;
public Draw()
{
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
tfInfo = new JTextField("Color & Shapes", 15);
lblColor = new JLabel("Colors:");
cbRed = new JCheckBox("Red");
cbBlue = new JCheckBox("Blue");
lblShapes = new JLabel("Shapes:");
shapes = new ButtonGroup();
rbCircle = new JRadioButton("Circle");
rbSquare = new JRadioButton("Square");
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
add(tfInfo);
add(lblColor);
add(cbRed);
add(cbBlue);
add(lblShapes);
add(rbCircle);
add(rbSquare);
add(btnSubmit);
shapes.add(rbCircle);
shapes.add(rbSquare);
}
public static void main(String [] args)
{
Draw n = new Draw();
n.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == btnSubmit)
{
if(cbRed.isSelected()&&cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("urple Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Purple Square");
}
}
else if(cbRed.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Red Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Red Square");
}
}
else if(cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Blue Circle");
}
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Blue Square");
}
}
}
public class MApp extends JPanel implements MouseListener
{
private boolean clicked;
private Rectangle r;
public MApp()
{
clicked = false;
r = new Rectangle(10, 10, 50, 50);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
if(clicked)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
}
g.fillRect((int)r.getX(), (int)r.getY(),
(int)r.getWidth(), (int)r.getHeight());
}
public void mouseClicked (MouseEvent e)
{
Point p = new Point(e.getX(),e.getY());
if(r.contains(p))
{
clicked = !clicked;
}
repaint();
}
public void mousePressed (MouseEvent evnt) {}
public void mouseReleased (MouseEvent evnt) {}
public void mouseEntered (MouseEvent evnt) {}
public void mouseExited (MouseEvent evnt) {}
}
}
你應該看看[創建一個GUI與JFC/Swing](http://docs.oracle.com。 com/javase/tutorial/uiswing /),[如何使用按鈕,複選框和單選按鈕](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html),[如何使用按鈕,複選框和單選按鈕]編寫Action Listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html),[在AWT和Swing中繪製](http://www.oracle.com/technetwork/java /painting-140037.html)和[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer
Swing(和大多數GUI)的工作方式與基於控制檯的程序不同,這是因爲它們是事件驅動的,並且有很多正在發生的「黑盒子」代碼(比如實際調用'actionPerformed'方法的方式)。事實上,大多數情況下,你並不在乎。你知道你可以在控件上添加一個'ActionListener',比如'JButton',當發生規定的事件時(比如用戶點擊按鈕),它會調用你的'actionPerformed'方法 – MadProgrammer