dcsimg
easy beginner question
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Claudia_Bretscher
Posted On:   Thursday, April 1, 2004 01:20 PM

Hi! I am all new to Swing and I want to build a GUI for a simple Chat Program.....but how can I display text and Smileys in the display by pushing the sending button? It just doesn't work the way I did. Another Problem are the gif Buttons.....they don't appear in the GUI although I programmed it.....thank's for your help! import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.JPanel.*; public class MainGUI { public JTextField display; public JTextField textField; public static void main(String[] args) { MenuFrame frame = new MenuFrame(); frame.setDefault   More>>

Hi! I am all new to Swing and I want to build a GUI for a simple Chat Program.....but how can I display text and Smileys in the display by pushing the sending button? It just doesn't work the way I did. Another Problem are the gif Buttons.....they don't appear in the GUI although I programmed it.....thank's for your help!

			
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel.*;

public class MainGUI

{

public JTextField display;
public JTextField textField;




public static void main(String[] args)
{
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}


class MenuFrame extends JFrame
{
public MenuFrame()
{
setTitle("CHAT");
setSize(600,600);




ActionListener insert = new InsertAction();
Container contentPane = getContentPane();
JPanel panel = new JPanel();
JPanel centerpanel = new JPanel();
JPanel AreaPanel = new JPanel();
JButton sendButton = new JButton("Send");
JTextField textField = new JTextField("",40);
JTextField display = new JTextField("",40);
display.setEditable(false);

panel.add(sendButton,insert);
panel.add(textField);
centerpanel.add(display);
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.add(centerpanel, BorderLayout.CENTER);

JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem connectItem = fileMenu.add(new TestAction("Connect"));



JMenuItem openItem = fileMenu.add(new TestAction("Buddylist"));
openItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_O, InputEvent.CTRL_MASK));

fileMenu.addSeparator();

saveItem = fileMenu.add(new TestAction("Save"));
saveItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S, InputEvent.CTRL_MASK));

fileMenu.addSeparator();

fileMenu.add(new
AbstractAction("End")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});




readonlyItem = new JCheckBoxMenuItem("Away");

ButtonGroup group = new ButtonGroup();

JRadioButtonMenuItem MotifSkinItem
= new JRadioButtonMenuItem("Motif Skin");
MotifSkinItem.setSelected(true);
JRadioButtonMenuItem MetalSkinItem
= new JRadioButtonMenuItem("Metal Skin");

group.add(MotifSkinItem);
group.add(MetalSkinItem);



Action cutAction = new TestAction("Cut");
cutAction.putValue(Action.SMALL_ICON,
new ImageIcon("cut.gif"));
Action copyAction = new TestAction("Copy");
copyAction.putValue(Action.SMALL_ICON,
new ImageIcon("copy.gif"));
Action pasteAction = new TestAction("Paste");
pasteAction.putValue(Action.SMALL_ICON,
new ImageIcon("paste.gif"));
JButton toolbar = new JButton("Smiley Toolbar");



JMenu editMenu = new JMenu("Edit");
editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
editMenu.add(toolbar);



JMenu optionMenu = new JMenu("Optionen");

optionMenu.add(readonlyItem);
optionMenu.addSeparator();
optionMenu.add(MotifSkinItem);
optionMenu.add(MetalSkinItem);

editMenu.addSeparator();
editMenu.add(optionMenu);

JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');

JMenuItem indexItem = new JMenuItem("Index");
indexItem.setMnemonic('I');
helpMenu.add(indexItem);

Action aboutAction = new TestAction("Info");
aboutAction.putValue(Action.MNEMONIC_KEY,
new Integer('O'));
helpMenu.add(aboutAction);

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);



popup = new JPopupMenu();
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);

getContentPane().addMouseListener(new
MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
if (event.isPopupTrigger())
popup.show(event.getComponent(),
event.getX(), event.getY());
}
});
}

public static final int WIDTH = 300;
public static final int HEIGHT = 200;

private JMenuItem saveItem;
private JMenuItem saveAsItem;
private JCheckBoxMenuItem readonlyItem;
private JPopupMenu popup;
}




class TestAction extends AbstractAction
{
public TestAction(String name) { super(name); }

public void actionPerformed(ActionEvent event)
{
System.out.println(getValue(Action.NAME)
+ " gewählt.");
}

}

class InsertAction extends MainGUI implements ActionListener{
public void actionPerformed(ActionEvent event){
String input = event.getActionCommand();
display.setText(textField.getText());

}
}


   <<Less

Re: easy beginner question

Posted By:   Craig_Wood  
Posted On:   Thursday, April 1, 2004 08:59 PM


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JPanel.*;

public class MainGUI
{
public JTextField display;
public JTextField textField;

public static void main(String[] args)
{
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class MenuFrame extends JFrame
{
public MenuFrame()
{
setTitle("CHAT");
setSize(600,600);

JPanel panel = new JPanel();
JPanel centerpanel = new JPanel();
JPanel AreaPanel = new JPanel();
final JTextField textField = new JTextField("",40);
final JTextField display = new JTextField("",40);
display.setEditable(false);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
display.setText(textField.getText());
}
});

panel.add(sendButton);
panel.add(textField);
Container contentPane = getContentPane();
centerpanel.add(display);
contentPane.add(panel, BorderLayout.SOUTH);
contentPane.add(centerpanel, BorderLayout.CENTER);

JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem connectItem = fileMenu.add(new TestAction("Connect", null));

JMenuItem openItem = fileMenu.add(new TestAction("Buddylist", null));
openItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_O, InputEvent.CTRL_MASK));

fileMenu.addSeparator();

saveItem = fileMenu.add(new TestAction("Save", null));
saveItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S, InputEvent.CTRL_MASK));

fileMenu.addSeparator();

fileMenu.add(new AbstractAction("End")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});

readonlyItem = new JCheckBoxMenuItem("Away");

ButtonGroup group = new ButtonGroup();

JRadioButtonMenuItem MotifSkinItem = new JRadioButtonMenuItem("Motif Skin");
MotifSkinItem.setSelected(true);
JRadioButtonMenuItem MetalSkinItem = new JRadioButtonMenuItem("Metal Skin");

group.add(MotifSkinItem);
group.add(MetalSkinItem);

Action cutAction = new TestAction("Cut", new ImageIcon("images/gifIcon.gif"));
// cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
Action copyAction = new TestAction("Copy", new ImageIcon("images/jpgIcon.gif"));
// copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
Action pasteAction = new TestAction("Paste", new ImageIcon("images/gifIcon.gif"));
// pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));
JButton toolbar = new JButton("Smiley Toolbar");

JMenu editMenu = new JMenu("Edit");
editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
editMenu.add(toolbar);

JMenu optionMenu = new JMenu("Optionen");
optionMenu.add(readonlyItem);
optionMenu.addSeparator();
optionMenu.add(MotifSkinItem);
optionMenu.add(MetalSkinItem);

editMenu.addSeparator();
editMenu.add(optionMenu);

JMenu helpMenu = new JMenu("Help");
helpMenu.setMnemonic('H');

JMenuItem indexItem = new JMenuItem("Index");
indexItem.setMnemonic('I');
helpMenu.add(indexItem);

Action aboutAction = new TestAction("Info", null);
aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('O'));
helpMenu.add(aboutAction);

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);

popup = new JPopupMenu();
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);

getContentPane().addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
if (event.isPopupTrigger())
popup.show(event.getComponent(), event.getX(), event.getY());
}
});
}

public static final int WIDTH = 300;
public static final int HEIGHT = 200;

private JMenuItem saveItem;
private JMenuItem saveAsItem;
private JCheckBoxMenuItem readonlyItem;
private JPopupMenu popup;
}

class TestAction extends AbstractAction
{
public TestAction(String name, Icon icon) {
super(name, icon);
}

public void actionPerformed(ActionEvent event)
{
System.out.println(getValue(Action.NAME) + " gewählt.");
}
}
About | Sitemap | Contact