KonstrPZ_FPO_LR2014_4LR
.pdf8. . ' B *, B
?
9.*?
10.r ' B q, B B *?
4 A 1 1 B 1 A 4 1 1 A D1 N D 1N O
SWIRG
4.1 J54K
8 B * * SWING.
4.2 5 5 5 <5 3 5 5!5
= ' ' B
*.
? B * ,
B JFrame, m '
C .
4.2.1 T45 5 Swing
; B B, B
*.
. |
|
|
JFrame |
|
., /i 2<. |
JButton |
|
. |
JPanel |
|
& |
JProgressBar |
|
Progress bar |
|
|
|
JTextArea |
|
& |
JTextField |
|
& |
JLabel |
|
9 B |
JCheckBox |
|
& B |
4.2.2 5 ! |
|
B F /, m q
:
|
|
|
|
setSize |
C B m |
|
|
pack() |
= / |
|
|
|
q, |
|
|
|
m |
|
|
|
. |
|
|
setVisible(bool vis) |
? m |
B |
|
|
B |
|
|
setText(string text) |
|
||
Add(component) |
? |
||
setBackgroundColor(Color) |
* |
|
|
addActionListener(actionListener) |
? B |
||
|
m |
|
|
4.2.3 ActionListener |
|
|
|
< ActionListener - ,
actionPerformed, B B ' B
static ActionListener buttonClick = new ActionListener() { public void actionPerformed(ActionEvent e) {
/*
* Z = 7, : < S 97 < ; 7 >. */
}
};
4.2.4 InvokeLater
invokeLater swing
B , B
, B. = :
public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
createFrame();
}
});
? q B, ' /
B , textField: 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.JTextField;
public class TestFrame extends JFrame {
static ActionListener buttonClick = new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.print(text.getText());//` ; : 7
< 97
}
};
/**
*
*/
static JFrame frame; static JPanel panel; static JTextField text;
private static final long serialVersionUID = 5671798241966272024L;
private static void createFrame()
{
/*Creating the frame*/
frame = new JFrame("Test frame");
// frame.setPreferredSize(new Dimension(260, 225)); frame.setSize(250,250);
//frame.pack(); frame.setVisible(true);
/*Creating the Panel*/ panel = new JPanel(); panel.setSize(100, 100);
panel.setBackground(Color.RED); panel.setVisible(true);
/*Creating the textField*/ text = new JTextField();
text.setText("Input the message here"); text.setSize(200,20);
/*Creating the Button*/ JButton but = new JButton(); but.setText("Hello button");
but.addActionListener(buttonClick); but.setVisible(true);
/*Make a parent tree*/ panel.add(but);//add elements to panel panel.add(text);
frame.add(panel);
}
public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() { createFrame();
}
});
}
}
4.3 ! 5 3 5
4.3.12 B ,
m , '
/ .
4.3.2D B B
*.
4.3.3=B /i :
1.< , B / (' Enter)
(text) B , text.(? m B ,
, /).
2.=B B .
D B :
1.< ', ' /
B
(< – Math.random()).
2.< ', B
, ' / B
, B .
3.< ', B
, ' / B
, B .
4.< ', B
, ' / B
, B .
5.< ', B
, ' / B
, B .
6.< ', B
, ' / B
, B .
7.< ', B
, ' / B
i /.
8.< ', B
, ' / B
/ .
9.< ', B
, ' / B
, .
10.< ', , ' / / (panel)
'C B (Color.YELLOW).
4.4 !5 L 5 <M
4.4.19 B.
4.4.2.
4.4.3. .
4.4.4. m, B /
B, B B B.
4.4.5 =B B, ' i B B B
(5 q. B ), ' ' /i B
q, .
4.5 4K 5
4.5.1 . i Swing? 4.5.2• Swing * ? 4.5.3 . m B Swing /
B B?
4.5.4& ' B B
main, invokeLater?
4.5.5r ActionListener?
4.5.6' q * ?
4.5.7r JTextArea JTextField?
4.5.8. , , * B?
4.5.9r JFrame?
4.5.10. H?