JButtons with multiple lines
I am having trouble using the JButtons. I'm trying to create buttons that have two lines of text in them (a string with a newline character in it), but for some reason the text on the button always reads as one line, even though the string itself prints out as two.
Here is a sample of the code:
publicclass Windowextends JFrame{
private GridBagLayout layout;
private GridBagConstraints constraints;
public Window()
{
super("Sonic Point of Sale");
layout =new GridBagLayout();
setLayout(layout);
constraints =new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 0;
constraints.weighty = 0;
JButton[] buttons =new JButton[320];
int count = 0;
String aString =new String("h \ni");
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 20; j++)
{
buttons[count] =new JButton(aString);
buttons[count].setBackground(Color.BLUE);
addComponent(buttons[count], i, j, 1, 1);
count++;
}
}
System.out.println(aString);
}
privatevoid addComponent( Component component,int row,
int column,int width,int height)
{
constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
add(component);
}
}
[2545 byte] By [
Shmigglea] at [2007-11-26 12:52:27]

# 4
What about this one?
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame{
public Window()
{
super("Sonic Point of Sale");
setDefaultCloseOperation(EXIT_ON_CLOSE);
String aString="line1 \nline2";
System.out.println(aString);
customButton cb=new customButton(aString);
cb.setPreferredSize(new Dimension(100,100));
getContentPane().add(cb,BorderLayout.NORTH);
setSize(500,500);
}
public static void main(String args[]){
new Window().setVisible(true);
}
class customButton extends JButton{
String str="";
customButton(){
super();
}
customButton(String s){
super();
str=s;
}
public void paintComponent(Graphics g){
if(!str.equals("")){int k=str.indexOf('\n');
String s=str.substring(0,k-1);
g.drawString(s,5,15);
s=str.substring(k,str.length());
g.drawString(s,5,35);
}
}
}
}
No Dukes.....!!!
# 8
>>I gave you some of the reasons why...
Please.....
Do you mean the change in look & Feel...?
Check now only one modification.....
import javax.swing.*;
import java.awt.*;
import javax.swing.plaf.*;
public class Window extends JFrame{
public Window()
{
super("Sonic Point of Sale");
setDefaultCloseOperation(EXIT_ON_CLOSE);
String aString=new String("line1 \n line2");
System.out.println(aString);
customButton cb=new customButton(aString);
JButton jb=new JButton("Another normal");
cb.setPreferredSize(new Dimension(100,100));
getContentPane().add(cb,BorderLayout.NORTH);
getContentPane().add(jb,BorderLayout.SOUTH);
setSize(500,500);
}
public static void main(String args[]){
new Window().setVisible(true);
}
class customButton extends JButton{
String str="";
customButton(){
super();
}
customButton(String s){
super();
str=s;
}
public void paintComponent(Graphics g){
if(!str.equals("")){
super.paintComponent(g);
int k=str.indexOf('\n');
String s=str.substring(0,k-1);
g.drawString(s,5,15);
s=str.substring(k+1,str.length());
g.drawString(s,2,35);
}
}
}
}