Techiio-author
Started by Swarnalata RoydebApr 15, 2022

Open
I want to show results as GUI and not just console can anyone help me in this!

3 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 3 VIEWES SHARE

I have a random graph program in Java.

Like this

blogpost

please can anyone solve this?

1 Replies

Techiio-commentatorPrince Mishra replied 6 days ago0 likes0 dislikes

Here is some example that I wrote to help you start with Swing. This is simple example of one JFrame creation with two JButtons and one JTextField. You may be also interested in MVC pattern, you can read more about that specific topic here (http://pclc.pace.edu/~bergin/mvc/mvcgui.html) Also if you want to show results maybe you should try with JTextPane control, but that's just my opinion

public class MainWindowClient implements ActionListener {

JFrame frame;
JTextField jtxInput;

JButton btnConnect;
JButton btnDisconnect;

public MainWindowClient() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
init();
}
});
}

public void init() {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
catch (UnsupportedLookAndFeelException e) {}

frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("Client");
frame.setSize(800, 600);

final JPanel title = new JPanel(new FlowLayout(FlowLayout.LEFT));
title.setBackground(new Color(255, 255, 255));
final JLabel lblAppName = new JLabel("Client Application");
lblAppName.setFont(new Font("sansserif", Font.PLAIN, 22));
title.add(lblAppName);
title.setBorder(BorderFactory.createTitledBorder(""));

final JPanel panelInputBoard = new JPanel(new GridLayout());
panelLogArea.setBorder(BorderFactory.createTitledBorder("Input"));
jtxInput = new JTextField("");

panelLogArea.add(jtxInput);

final JPanel panelCommandBoard = new JPanel(new FlowLayout(FlowLayout.LEFT));
panelCommandBoard.setBorder(BorderFactory.createTitledBorder("Client Commands"));

btnConnect = new JButton("Connect");
btnConnect.addActionListener(this);

btnDisconnect = new JButton("Disconnect");
btnDisconnect.addActionListener(this);

panelCommandBoard.add(btnConnect);
panelCommandBoard.add(btnDisconnect);

frame.add(title, BorderLayout.NORTH);
frame.add(panelCommandBoard, BorderLayout.SOUTH);
frame.add(panelInputBoard, BorderLayout.NORTH);

frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent event) {
JButton eventSource = (JButton) event.getSource();
if(eventSource.getText().equals("Connect")) {
// Do some stuff
}
if(eventSource.getText().equals("Disconnect")) {
// Do some stuff
}
}


public static void main(String[] args) {
MainWindowClient m = new MainWindowClient();
}
}

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance