Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How can I get the GridLayout to work in this program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MemoSaver2 extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;
private JTextArea theText;
private String memo1 = "No Memo 1.";
private String memo2 = "No Memo 2.";
private String memo3 = "No Memo 3.";
private String memo4 = "No Memo 4.";

public MemoSaver2()
{
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Memo Saver");
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2, 3));
//this.setButton(MemoSaver2);
contentPane.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
buttonPanel.setLayout(new FlowLayout());
JButton memo1Button = new JButton("Save Memo 1");
memo1Button.addActionListener(this);
buttonPanel.add(memo1Button);
JButton memo2Button = new JButton("Save Memo 2");
memo2Button.addActionListener(this);
buttonPanel.add(memo2Button);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
JButton memo3Button = new JButton("Gary Davis");
memo3Button.addActionListener(this);
buttonPanel.add(memo3Button);
JButton get1Button = new JButton("Get Memo 1");
get1Button.addActionListener(this);
buttonPanel.add(get1Button);
JButton get2Button = new JButton("Get Memo 2");
get2Button.addActionListener(this);
buttonPanel.add(get2Button);
JButton get3Button = new JButton("Get Gary Davis");
get3Button.addActionListener(this);
buttonPanel.add(get3Button);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
//contentPane.add(buttonPanel, GridLayout);
JPanel textPanel = new JPanel();
textPanel.setBackground(Color.BLUE);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.WHITE);
textPanel.add(theText);
contentPane.add(textPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save Memo 1"))
{
memo1 = theText.getText();
theText.setText("Memo 1 saved.");
}
else if (actionCommand.equals("Save Memo 2"))
{
memo2 = theText.getText();
theText.setText("Memo 1 saved.");
}
else if (actionCommand.equals("Gary Davis"))
memo3 = theText.getText();
else if (actionCommand.equals("Clear"))
theText.setText("");
else if (actionCommand.equals("Get Memo 1"))
theText.setText(memo1);
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2);
else
theText.setText("Error in memo interfact.");
}
public static void main(String[]args)
{
MemoSaver2 guiMemo = new MemoSaver2();
guiMemo.setVisible(true);
}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the Swing forum.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gary davis:

contentPane.setLayout(new GridLayout(2, 3));
//this.setButton(MemoSaver2);
contentPane.setLayout(new BorderLayout());
}


Please use the UUB Code tags to make your code readable.
And you are actually setting the layout to BorderLayout.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic