Valhalla Legends Forums Archive | Java Programming | GUI Help

AuthorMessageTime
Spilled[DW]
Hi, im creating a GUI on my JFrame and im having some complications with the TextArea for the chat interface and am in need of your help again :) Heres some Code:

JavaBot.java -
[code]
import javax.swing.JFrame;
import java.awt.Graphics;
import java.io.BufferedInputStream;
import java.net.Socket;

public class JavaBot extends JFrame {

private Graphics g;

public static void main(String[] args)
{

JavaBot jBot = new JavaBot();
jBot.init();
}

public void init()
{

/* Start - Create Frames from MenuFrame.java */
MenuFrame menuFrame = new MenuFrame();
menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuFrame.setSize(700,400);
menuFrame.setVisible(true);
/* End - Create Frames from MenuFrame.java */


attemptConnect();
}

/*
* this is the main thread loop
*/
public void attemptConnect() {
String server = "63.241.83.8";
int port = 6112;
/*
* move the calls to create the socket out of the loop
* so we only do it once.
* we also move the buffered reader so we dont keep
* re-assigning it
*/
Socket wSock;
BufferedInputStream inData;
try {
wSock = new Socket(server, port);
System.out.println("connected to [" + wSock + "]");

/*
* we then create a buffered input stream to read any data
*/
inData = new BufferedInputStream(wSock.getInputStream());
}
catch(Exception e)
{
System.out.println("Error connecting to host - " + e);
return;
}

while(true) {
try {
/*
* this will wait for the client to send data, or if they disconnect
* it will return -1
*/
int firstByte = inData.read();
if(firstByte == -1) {
/*
* this 'break' will exit our read 'while(true)' loop
*/
//g.drawString("die!", 60, 70);
System.out.println("connection lost");
break;
}else{
byte[] data = new byte[inData.available() + 1];
data[0] = (byte)firstByte;
inData.read(data, 1, data.length-1);
String strData = new String(data);
// g.drawString("battle.net sent [" + strData + "]", 60, 70);
System.out.println("got: " + strData);

/*
* here we have removed the close connection, and instead wait for
* the server to close the connection.
*/
//wSock.close();
}
}
catch(Exception e) {
//g.drawString("Error: " + e, 100, 110);
System.out.println("Error: " + e);
break;
}
}
/*
* at this point the server has closed the connection
* or an error has occured and you can perform post-connection
* operations
*/
System.out.println("stopped reading");
}
}
[/code]

MenuFrame.java

[code]
import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;


public class MenuFrame extends JFrame
{
private final Color colorValues[] = {Color.BLACK, Color.BLUE, Color.RED, Color.GREEN};
private JRadioButtonMenuItem colorItems[];
private JRadioButtonMenuItem fonts[];
private JCheckBoxMenuItem styleItems[];
private JLabel displayJLabel;
private ButtonGroup fontButtonGroup;
private ButtonGroup colorButtonGroup;
private int style;
private GridBagConstraints constraints = new GridBagConstraints();
private GridBagLayout layout = new GridBagLayout();


public MenuFrame()
{
super("My Java Bot - Designed By: Spilled]WaR[ - ");
setLayout(layout);
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('f');

/* Connect Menu Item */
JMenuItem connectItem = new JMenuItem("Connect...");
connectItem.setMnemonic('C');

fileMenu.add(connectItem);
connectItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(MenuFrame.this, "Connect Clicked - Connect!", "Connect", JOptionPane.PLAIN_MESSAGE);

}
}
);
/* End - Connect Menu Item */

/* Disconnect Menu Item */
JMenuItem disconnectItem = new JMenuItem("Disconnect...");
connectItem.setMnemonic('C');

fileMenu.add(disconnectItem);
disconnectItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(MenuFrame.this, "Connect Clicked - Disconnect!", "Disconnect", JOptionPane.PLAIN_MESSAGE);

}
}
);
/* End - Disconnect Menu Item */

fileMenu.addSeparator(); // Add Separator

/* Start - Exit Menu Item */
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');

fileMenu.add(exitItem);
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//JOptionPane.showMessageDialog(MenuFrame.this, "Connect Clicked - Exit!", "Exit", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
);
/* End - Exit Menu Item */

/* Start - Create the Menu Bar */
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
/* End - Create the Menu Bar */

getContentPane().setBackground(Color.BLACK);

JTextArea chatText = new JTextArea("HI!!!",5, 10);
constraints.fill = GridBagConstraints.BOTH;
addComponent(chatText, 0, 0, 1, 3);

}
private void 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);
}
}
[/code]

As you can see im using addComponent to determine the width, height, column, and row. Any ideas anyone?
October 1, 2005, 7:57 AM
gameschild
If you want to design Java Interfaces quickly i would advice using Eclipse with the Visual Editor, once you have created what you want make sure you read the code to see what it has done and learn it. That way you can continue to use the Visual Editor but understand how it works to tweak or write code without it.
October 2, 2005, 7:18 PM
Dynobird
[quote author=gameschild link=topic=12944.msg129850#msg129850 date=1128280709]
once you have created what you want make sure you read the code to see what it has done and learn it. That way you can continue to use the Visual Editor but understand how it works to tweak or write code without it.
[/quote]
Yes. Because your GUI objects are going to have to talk with others, and you're going to have to handle these conversations efficiently, in an OOP sense. By OOP conventions, your GUI's interaction with other objects should be kept at a minimum, so that when changes have to be made you can isolate them to the least amount of places possible. When you make complex applications, with big GUIs, this becomes more and more important. This is why you should become familiar with Swing.
October 13, 2005, 5:56 PM

Search