jpanel

How to make a rectangle with mouse listeners and add them to JFrame?

橙三吉。 提交于 2019-12-11 22:47:57
问题 Basically, I want to make a rectangle that contains a mouse listener for my tic tac toe game. I intend to place them in between the lines to serve as mouse listeners. I have already set up a JFrame, all that is left is for me to add the rectangle. How would I go about doing this? Here is my code so far: public class TicTacToe extends JFrame{ public static void main(String[] args) { // TODO Auto-generated method stub new TicTacToe(); } public TicTacToe(){ //Sets up the frame this.setTitle("Tic

Jpanel inside another Jpanel

女生的网名这么多〃 提交于 2019-12-11 22:39:25
问题 I am facing a problem that i think its pretty easy to solve but i cannot figure it out. So i have a main jpanel and i cannot insert another jpanel2 in a different class to it, the components from jpanel2 dont appear on the 1st one. Here's the code for the 1st jpanel constructor: public PainelPrincipal(Jogo janela) {` super(); painel = new JPanel(){ protected void paintComponent(java.awt.Graphics g) { super.paintComponents(g); try{ g.drawImage(ImageIO.read(PainelPrincipal.class.getResource(

Why is the class CanvasPane not included in the Java API?

倖福魔咒の 提交于 2019-12-11 20:27:21
问题 ** Why is the class CanvasPane not included in the Java API?. import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** * Class Canvas - a class to allow for simple graphical * drawing on a canvas. * * @author Michael Kolling (mik) * @author Bruce Quig * * @version 2008.03.30 */ public class Canvas { private JFrame frame; private CanvasPane canvas; private Graphics2D graphic; private Color backgroundColor; private Image canvasImage; /** * Create a Canvas with default height, width

overlapping JPanels in the GUI

回眸只為那壹抹淺笑 提交于 2019-12-11 18:52:21
问题 I am using BorderLayout in my application. I have a main panel to which I add two JPanels at the center. I want one of them to be transparent. My code is : mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(getGraphPaneScrollPane(), BorderLayout.CENTER); mainPanel.add(getSituationPanel(), BorderLayout.CENTER); Code for these two functions is : public JScrollPane getGraphPaneScrollPane() { if (graphPaneScrollPane == null) { graphPaneScrollPane = new JScrollPane();

Clicking a button within a JFrame passes an data to a JPanel

夙愿已清 提交于 2019-12-11 18:32:24
问题 I have a Jframe with two buttons: '1' and '2'. Clicking the button '1' should display the capital letter A in the JPanel. Code fore my JFrame: import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class DrawFrame extends JFrame{ private final int WIDTH = 500; private final int HEIGHT = 300; private JButton number1; private

How to add a JPanel repeatedly using an 'ADD' button? How to call a JPanel in the ActionListener()?

江枫思渺然 提交于 2019-12-11 17:22:44
问题 I want to repeat the Employment History JPanel(with the contents in grey), by using the ADD button and remove it by using Remove button.This is the GUI I would also like to know how to store the values of the textfields temporarily. Will an array be helpful? Here is the code JLabel lblEmploymentHistory = new JLabel("Employment History:"); lblEmploymentHistory.setBounds(46, 145, 128, 14); frame.getContentPane().add(lblEmploymentHistory); JPanel panelemp = new JPanel(); panelemp.setBounds(46,

How to determine which panel holds a button that was pressed - Application Window

丶灬走出姿态 提交于 2019-12-11 17:19:45
问题 I have an array of Tile objects (Panel, Button to a tile) in a JPanel, 20x20. If button 1 is clicked, a thing happens, button 2 is pressed, a thing happens, etc. I want a specific function to happen every time a button other than one in the top row is clicked (the top row of buttons all have functions assigned, the other 380 buttons do not have assigned functions). So in the top buttons' cases I have the code: if(e.getSource() == tiles[0][0].button) { //do stuff } else if(e.getSource() ==

Incorrect JPanel position in GridBagLayout

自古美人都是妖i 提交于 2019-12-11 16:45:53
问题 im using GridBag to display some JPanels with images inside a JScrollPane. When there are 3 or more images the GridBagConstraints work ok but when i have 1 or 2, they get aligned to the center of the JScrollPane instead of being in their position in the top (like in a gallery) Here is my code: JPanel jPanel1 = new JPanel(); GridBagLayout layout = new GridBagLayout(); jPanel1.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); JPanel photo = new JPanel(); Dimension d = new

Using JPanel to make a calculator: how to use layout?

纵然是瞬间 提交于 2019-12-11 16:21:50
问题 I'm working on a question that simply states to make an GUI that looks like This calculator, it doesn't have to function, just look like it. So I think I have the JPanel and JButton components right but I can't organize the fields to make it come out right. I'm pretty new so any crash course on how to organize a GUI would be appreciated. Here's what I have so far: // Using a JPanel to help lay out components. import java.awt.GridLayout; import java.awt.BorderLayout; import javax.swing.JFrame;

How to create obstacle in swing

青春壹個敷衍的年華 提交于 2019-12-11 15:29:58
问题 I want to create this obstacle in red color in Java class Grid extends JFrame{ public Grid(){ setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(250,300); // Set the width,height,number of rows and columns final GridPanel panel = new GridPanel(300, 300, 10, 10); add(panel); } } class GridPanel22 extends JPanel{ int width, height, rows, columns; int gridWidth, gridHeight; public GridPanel22(int width, int height, int rows, int columns){ gridHeight = height / rows; gridWidth