:
- , , .
:
Java Swing AWT. , , , , .
TextFields - ( ). , , , .
JTextField:
JTextFieldjta = new JTextField (10);
10, , . JText getText(). setText(String s).
, .
classLabExample extends JFrame { JTextFieldjta = new JTextField(10); Font fnt = new Font("Times new roman",Font.BOLD,20); LabExample() { super("Example"); setLayout(new FlowLayout()); setSize(250,100); add(jta); jta.setForeground(Color.PINK); jta.setFont(fnt); setVisible(true); } public static void main(String[]args) { newLabExample(); } } |
, , , JTextField . , .
, :
importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; classLabExample extends JFrame { JTextField jta1 = new JTextField(10); JTextField jta2 = new JTextField(10); JButton button = new JButton(" Add them up"); Font fnt = new Font("Times new roman",Font.BOLD,20); LabExample() { super("Example"); setLayout(new FlowLayout()); setSize(250,150); add(new JLabel("1st Number")); add(jta1); add(new JLabel("2nd Number")); add(jta2); add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEventae) { try { double x1 = Double.parseDouble(jta1.getText().trim()); double x2 = Double.parseDouble(jta2.getText().trim()); JOptionPane.showMessageDialog(null, "Result = "+(x1+x2),"Alert",JOptionPane.INFORMATION_MESSAGE); } catch(Exception e) { JOptionPane.showMessageDialog(null, "Error in Numbers!","alert", JOptionPane.ERROR_MESSAGE); } } }); setVisible(true); } public static void main(String[]args) { newLabExample(); } } |
|
|
JTextArea
TextAreas TextFields, . TextArea ,
importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; classTextAreaExample extends JFrame { JTextArea jta1 = new JTextArea(10,25); JButton button = new JButton("Add some Text"); publicTextAreaExample() { super("Example"); setSize(300,300); setLayout(new FlowLayout()); add(jta1); add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEventae) { String txt = JOptionPane.showInputDialog(null,"Insert some text"); jta1.append(txt); } }); } public static void main(String[]args) { newTextAreaExample().setVisible(true); } } |
, JScrollPane :
JTextAreatxtArea = new JTextArea(20,20) JScrollPanejScroll = new JScrollPane(txtArea); // add(Scroll); // we add the scrollPane and not the text area. |
!
Layout :
BorderLayout:
(WEST, EAST, NOTH, SOUTH and Center). .
, BorderLayout :
add(comp, BorderLayout.EAST);
, JPanel . JPanel
GridLayout
GridLayout , .
1 2 3 4
5 6 7 8
9 10 11 12
GridLayout 3 4 , , , .
importjavax.swing.*; import java.awt.*; importjava.awt.event.*; classBorderExample extends JFrame { JPanel[] pnl = new JPanel[12]; publicBorderExample() { setLayout(new GridLayout(3,4)); for(inti = 0; i<pnl.length; i++) { int r = (int) (Math.random() * 255); int b = (int) (Math.random() * 255); int g = (int) (Math.random() * 255); pnl[i] = new JPanel(); pnl[i].setBackground(new Color(r,g,b)); add(pnl[i]); } pnl[4].setLayout(new BorderLayout()); pnl[4].add(new JButton("one"),BorderLayout.WEST); pnl[4].add(new JButton("two"),BorderLayout.EAST); pnl[4].add(new JButton("three"),BorderLayout.SOUTH); pnl[4].add(new JButton("four"),BorderLayout.NORTH); pnl[4].add(new JButton("five"),BorderLayout.CENTER); pnl[10].setLayout(new FlowLayout()); pnl[10].add(new JButton("one")); pnl[10].add(new JButton("two")); pnl[10].add(new JButton("three")); pnl[10].add(new JButton("four")); pnl[10].add(new JButton("fve")); setSize(800,500); } public static void main(String[]args) { newBorderExample().setVisible(true); } } |
|
|
,
, JFrame GridLayout 3 4 (), JPanel (2, 1) BorderLayout. JPanel (3, 3) FLowLayout.
NullLayoutManager
. , , (setLayout ()). - :
importjavax.swing.*; import java.awt.*; importjava.awt.event.*; classNullLayout extends JFrame { JButton but1 = new JButton("One");; JButton but2 = new JButton("two");; JButton but3 = new JButton("three");; publicNullLayout() { setLayout(null); but1.setBounds(150,300,100,20); // added at 150,300 width = 100, height=20 but2.setSize(80,400); // added at 0,0 width = 80, height=400 but3.setLocation(300,100); but3.setSize(200,75); // those two steps can be combined in one setBounds method call add(but1); add(but2); add(but3); setSize(500,500); } public static void main(String[]args) { newNullLayout().setVisible(true); } } |
Java . Java
JMenuBar: , .
JMenu: .
JMenuItem: , .
Button ( MenuItems AbstractButton). ActionListener ,
1. , 20 , . Shape, : , .
2. , , .
3. , , , .
6