Displaying a Text Adventure in a GUI - java

I'm trying to put my text adventure in a GUI,and make it so there are no buttons just text and user input. This is the code for my GUI
import java.awt.*;
import javax.swing.*;
public class Window {
public static void createwindow() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Component textLabel = null;
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createwindow(); }
}
I have my text adventure in a separate class and was wondering if there was any easy way to display that in this GUI. Thanks for your help.

import javax.swing.*;
public class Window {
public static void createwindow() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Component textLabel = null;
JTextArea jta = new JTextArea();
frame.add(jta);
// frame.pack();
}
public static void main(String[] args) {
createwindow(); }
}
In my understanding, you just want a TextArea.Is right?

Related

How to change the background color of a JFrame? [duplicate]

This question already has answers here:
Setting background color for a JFrame
(14 answers)
Closed 3 years ago.
I have tried to change the background color of a JFrame and it just never works. Here is my code:
package com.company;
import javax.swing.*;
import java.awt.*;
public class Main extends JPanel
{
private static JFrame frame = new JFrame();
private static int rand = 3;
public static void main(String[] args)
{
frame.getContentPane().add(new Main());
frame.setSize(1960, 1070);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setResizable(false);
}
}
You'll need to go through getContentPane to change the frame's background color:
frame.getContentPane().setBackground(Color.BLACK);
Change the color of the JPanel you are adding, not the JFrame. You won't see the black JFrame if you add a white JPanel on top of it!
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ChangeColor extends JPanel {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
public static void createGUI() {
JFrame frame = new JFrame();
ChangeColor c = new ChangeColor();
c.setBackground(Color.BLACK);
frame.add(c);
frame.setSize(1960, 1070);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
}

Problems with components on InternalFrame

I am currently creating a GUI with internalFrame. This is how my code looks like:
import javax.swing.*;
public class GUI extends JFrame {
private JDesktopPane desktop;
private JMenuBar menuBar;
private JMenu startMenu;
private JMenuItem travelInfo;
private JButton updateData;
public GUI()
{
desktop = new JDesktopPane();
updateData = new JButton("Update");
createFrame();
setContentPane(desktop);
setJMenuBar(buildMenu());
}
private JMenuBar buildMenu()
{
menuBar = new JMenuBar();
startMenu = new JMenu("Start");
menuBar.add(startMenu);
travelInfo = new JMenuItem("TravelInfo");
startMenu.add(travelInfo);
return menuBar;
}
protected void createFrame()
{
NewFrame frame = new NewFrame();
frame.add(updateData);
frame.setVisible(true);
desktop.add(frame);
}
}
And then my other class:
import javax.swing.JInternalFrame;
class NewFrame extends JInternalFrame {
public NewFrame() {
setSize(800, 600);
}
}
And my main:
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
GUI frame = new GUI();
frame.setSize(800, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
What I want to do is to make the internalFrame that I create unsizeable so that it will always be as big as the JDesktopPane which is unsizeable. I am then going to add different menu option so that I can switch between the different frames so it kinda looks like a website. I am also wondering how I change the size of my button inside my internalFrame. I tried setting the frame.setResizable(false); for the internalFrame but it didn't work. Setting a size on the button didn't work out either... Somebody got an idea on how to solve this?

How to create a JFrame with a title

How do you create a JFrame with a title using Java Eclipse? I've tried using
package frame;
import javax.swing.JFrame;
public class Frame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setText("abc");
}
Perhaps using the setTitle method
frame.setTitle("abc");
or alternatively
JFrame frame = new JFrame("abc");
This is the way to create a JFrame with title:
JFrame frame = new JFrame("Title");
This is the way to create a JFrame and set its title:
JFrame frame = new JFrame();
frame.setTitle("Title");
And this is the first result of Google for create jframe with title
You could use .setTitle(); to set the title.
import javax.swing.*;
import javax.awt.*;
public class MyFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("abc");
frame.setSize(100,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

JComboBox not displayed

I'm trying to put a JComboBox on my JFrame, if i clicked shows the contents of the combo, but it was not showed on the JFrame.
I already tried with: .setVisible(true), .setEnabled(true), etc.
Here is my code:
public class tryCode {
private final JComboBox vehicleTypeBox = new JComboBox(new String[] {"HELLO WORLD", "OLA K ASE"});
private JFrame frame;
public tryCode() {
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(null);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
showComboBox();
}
public void showComboBox() {
vehicleTypeBox.setBounds(10,80,100,30);
vehicleTypeBox.setSelectedIndex(0);
frame.add(vehicleTypeBox);
}
}
Any solution is welcome!. Thanks
You never call showComboBox() anywhere. If it is supposed to show up at startup you should call it in the constructor.
public tryCode()
{
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(new FlowLayout()); // do not use null!
frame.setResizable(false);
frame.setLocationRelativeTo(null);
showComboBox();
frame.setVisible(true);
}
I just test your code with adding a main method.and it works.no problems.
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class TryCode {
private final JComboBox vehicleTypeBox = new JComboBox(new String[] {"HELLO WORLD", "OLA K ASE"});
private JFrame frame;
public TryCode() {
frame = new JFrame("");
frame.setSize(new Dimension(300, 300));
frame.setLayout(null);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
showComboBox();
}
public void showComboBox() {
vehicleTypeBox.setBounds(10,80,100,30);
vehicleTypeBox.setSelectedIndex(0);
frame.add(vehicleTypeBox);
}
/**
* #param args
*/
public static void main(String[] args) {
TryCode t=new TryCode();
}
}

Non-Working paintComponent method

Code :
import javax.swing.*;
import java.awt.*;
public class firstGUI extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
public void paintComponent(Graphics g) {
Image image = new ImageIcon("dist.jpg").getImage();
g.drawImage(image,0,0, this);
}
}
Compiles perfectly, but when I run it, it just shows a form. No picture(or any other operation in paintComponent) shows up. Is there something I'm missing?
Your paintComponent method is an instance method of your firstGUI class (a JPanel). The problem is that you are not creating an instance of firstGUI and adding it to the frame.
The following replacement main method instantiates firstGUI and adds it to the contentPane of the frame:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.getContentPane().add(new firstGUI());
frame.setVisible(true);
}

Categories