1. I am new to Swing and I am trying to put a JButton in to a JPanel. The JPanel is configured to work with BorderLayout. I want the panel to exactly fit the size of the button but I could not do it. The panel is large and there is ugly space sorrounding it.
2. I am trying to set the rows and columns of the text area. After I set them using setRows and setColumns of JTextArea methods, it shows in the console that they are set when I see them using getRows and getColumns but I could not actually see the text area widening up. It also affects the sizes of the labels...
Here is the Java code for the Interface.java file
Code:import java.io.*; import javax.swing.*; import java.awt.*; class JavaFrame extends JFrame{ JPanel newPanel1 = new JPanel(); JPanel newPanel2 = new JPanel(); JPanel newPanel3 = new JPanel(); JTextArea requestTextArea = new JTextArea(); JTextArea responseTextArea = new JTextArea(); JLabel requestLabel = new JLabel("Send commands to the mail server"); JLabel responseLabel = new JLabel("Received response from the mail server"); JButton sendRequest = new JButton("Send to server"); public JavaFrame(String title){ super(title); this.setDefaultLookAndFeelDecorated(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void addLabels(){ //set sizes for labels requestLabel.setPreferredSize(new Dimension(600, 50)); responseLabel.setPreferredSize(new Dimension(600, 50)); //set colors for labels requestLabel.setOpaque(true); responseLabel.setOpaque(true); requestLabel.setBackground(Color.YELLOW); responseLabel.setBackground(Color.YELLOW); //set labels for textareas requestLabel.setLabelFor(requestTextArea); responseLabel.setLabelFor(responseTextArea); //add labels to panels newPanel1.add(requestLabel); newPanel3.add(responseLabel); } void addButtons(){ newPanel2.add(sendRequest, BorderLayout.WEST); } void addTextAreas(){ //set colors for textareas requestTextArea.setBackground(new Color(200, 100, 100)); responseTextArea.setBackground(new Color(200, 100, 100)); requestTextArea.setForeground(Color.WHITE); responseTextArea.setForeground(Color.WHITE); //set rows and columns for textareas requestTextArea.setRows(15); System.out.println(requestTextArea.getRows()); System.out.println(requestTextArea.getColumns()); //add textareas to panels newPanel1.add(requestTextArea); newPanel3.add(responseTextArea); } void addComponentsToFrame(){ //set layouts and sizes for panels newPanel1.setLayout(new GridLayout(2,0)); //newPanel2.setLayout(new GridLayout(0,1)); newPanel3.setLayout(new GridLayout(2,0)); newPanel2.setPreferredSize(new Dimension(400, 0)); System.out.println(newPanel2.getSize().toString()); //Frame's layout and its components this.getContentPane().add(newPanel1); this.getContentPane().add(newPanel2); this.getContentPane().add(newPanel3); } } class Interface{ public static void main(String args[]){ JavaFrame newFrame = new JavaFrame("Mail Client"); //set frame's layout newFrame.setLayout(new GridLayout(3,0)); //add all components to panels and panels to frame newFrame.addLabels(); newFrame.addButtons(); newFrame.addTextAreas(); newFrame.addComponentsToFrame(); //frame's visibility and size newFrame.setVisible(true); newFrame.pack(); } }


Reply With Quote


Bookmarks