I am having trouble with a layout manager to lay out a gridbag but it's not working properly.
I am trying to have a combo box in grid (0,0)
a label in grid (1,0)
a text field in grid (0,1)
and a button in grid (1,1)
all the components are currently on 1 line...is this because of the JPanel?
also I have drawn 2 rectangles and I would like to fill them which I can do but I would like to fill them so there is still a border around the rectangles.
sjalle did help me with a similar problem but I am trying to make sense of it by recreating my own version which I can understand![]()
I got the examples combined i got from a book so I need someone to fill in the gaps
Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; public class ProgressBar extends JApplet implements Runnable, ActionListener { Thread runner; JButton play; JButton stop; JTextField text; JLabel textLabel; public void init() { Container contentArea = getContentPane(); GridLayout grid = new GridLayout(2,1); contentArea.setLayout(grid); contentArea.setBackground(Color.white); JPanel console = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints pos = new GridBagConstraints(); console.setLayout(gridbag); console.setBackground(Color.white); ProgressPanel animation = new ProgressPanel(); JComboBox standards = new JComboBox(); standards.addItem("Corfu"); standards.addItem("Kefalonia"); standards.addItem("Crete"); standards.addItem("Rhodes"); standards.addItem("Paxos"); standards.addItem("Mykonos"); pos.gridwidth = 3; pos.gridx = 0; pos.gridy = 0; textLabel = new JLabel("Fun programming :"); pos.gridx = 1; pos.gridy = 0; text = new JTextField("", 15); pos.gridx = 0; pos.gridy = 1; play = new JButton("Click To Start"); play.addActionListener(this); pos.gridx = 1; pos.gridy = 1; console.add(standards); console.add(play); console.add(textLabel); console.add(text); contentArea.add(console); contentArea.add(animation); setContentPane(contentArea); } public void actionPerformed(ActionEvent event) { if(event.getSource()==stop) stop(); if(event.getSource()==play) start(); } class ProgressPanel extends JPanel { public void paintComponent(Graphics painter) { Graphics2D painter2D = (Graphics2D) painter; painter2D.setColor(Color.black); Rectangle2D.Float progressBar1 = new Rectangle2D.Float(30F, 30F, 360F, 30F); painter2D.draw(progressBar1); painter2D.drawString("Theoretical Transfer Speed", 30, 25); Rectangle2D.Float progressBar2 = new Rectangle2D.Float(30F, 90F, 360F, 30F); painter2D.draw(progressBar2); painter2D.drawString("Realistic Transfer Speed", 30, 85); } } public void start() { if(runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if(runner != null) runner = null; } public void run() { while (runner == Thread.currentThread() ) { repaint(); try { Thread.sleep(100); } catch(InterruptedException e) { } } } }


Reply With Quote



Bookmarks