jframe

how can I scroll my JFrame using the JScrollbar?

情到浓时终转凉″ 提交于 2019-11-29 09:49:53
I have a problem. I have a JFrame with some JTextFields, JLabels, Jlists & JButtons now the contents of my frame is more than the screen area so I want to attach a JScrollBar to my JFrame but my srollbar does not work. So, can anyone please guide me on how I can scroll my JFrame using the JScrollbar? Put all the components in one panel (instead of in the JFrame) Add this panel to a JScrollPane Add the JScrollPane to your frame. I should be something like: JPanel container = new JPanel(); container.add(panel1); container.add(Panel2); JScrollPane jsp = new JScrollPane(container); frame.add(jsp);

How to center align the title in a JFrame?

你说的曾经没有我的故事 提交于 2019-11-29 09:36:09
I have a JFrame with a Title . I want center align the title , so that it appears in the middle of the JFrame's title . Thanks. Consider leaving the title left-justified...but...this will get you near the center. For resizable frames, you need to rewrite the title on resize. JFrame t = new JFrame(); t.setSize(600,300); t.setFont(new Font("System", Font.PLAIN, 14)); Font f = t.getFont(); FontMetrics fm = t.getFontMetrics(f); int x = fm.stringWidth("Hello Center"); int y = fm.stringWidth(" "); int z = t.getWidth()/2 - (x/2); int w = z/y; String pad =""; //for (int i=0; i!=w; i++) pad +=" "; pad

Java, JFrame: getWidth() returns 0

。_饼干妹妹 提交于 2019-11-29 09:30:21
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH); setResizable(false); setUndecorated(true); System.out.println("--------> "+getContentPane().getWidth()); //----> 0 why is this happening? I'm trying to determine the size of a JFrame. I've searched on google and checked the documentation and I still don't know why it won't work It works fine on about any other control I try it on EDIT: frame.getWidth() works when called outside of the class (that extends JFrame) still, if I replace System.out.println("--------> "+getContentPane().getWidth()); with System.out.println("--------> "+this

Can I create a BufferedImage from a JPanel without rendering in a JFrame?

假如想象 提交于 2019-11-29 09:29:58
Is it possible to create a BufferedImage from a JPanel without first rendering it in a JFrame? I've searched everywhere I can think of and cannot find an answer. Can anyone help? Here is some sample code. If I don't un-comment the JFrame code, my BufferedImage is blank. test(){ // JFrame frame = new JFrame(); JPanel panel = new JPanel(); Dimension dim = new Dimension(50,50); panel.setMinimumSize(dim); panel.setMaximumSize(dim); panel.setPreferredSize(dim); JLabel label = new JLabel("hello"); panel.add(label); // frame.add(panel); // frame.pack(); BufferedImage bi = getScreenShot(panel); //..

Easier way to make a paint application in java?

时间秒杀一切 提交于 2019-11-29 08:59:58
So basically I have some code I was working on a couple of days ago that is kind of like Paint, which allows you to essentially draw on the screen using the mouse. I kind of discovered this property by accident, and I realized that it is really inefficient and i'm wondering if there is a more practical way to do this. There isn't really any reason to give all of my code, but here are the important parts private static void createAndShowGui() { SimpleDraw mainPanel = new SimpleDraw(); MenuBar.createMenuBar(); JLabel label = new JLabel(); label.setText("Drawing prototype 0.0.1"); // label

Java - adding components to JFrame

て烟熏妆下的殇ゞ 提交于 2019-11-29 08:48:36
I've seen a couple of ways of doing theism they both seem to work but I'm just wondering if one is better practice over the other. For example, with a JFrame called myFrame you could do: myFrame.add(new JButton("OK")); And you can also do: Container c = myFrame.getContentPane(); c.add(new JButton("OK")); Is one of these 'correct'? A literal copy from the class javadoc of JFrame The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain

Java - Message when closing JFrame Window

↘锁芯ラ 提交于 2019-11-29 07:34:53
I have a Java Program containing a class Application inheriting from JFrame. I want to display a message which asks the user if he wants to exit the program upon clicking the X button at the top right of the window. This is my code so far: I got this code from a tutorial I found online. I coded the WindowClosing event handler myself. However, I have trouble registering the window listener (addWindowListener). It is telling me that WindowAdapter is abstract and cannot be instantiated. How can I solve this problem please? Dan D. Basically, you got it almost correct. There are a few things not

Changing The Underlying Background Color Of A Swing Window

我的未来我决定 提交于 2019-11-29 07:09:03
问题 As discussed here, when resizing a Swing application in Vista (and Windows 7, which is what I'm using) you get a black background in the right/bottom corner while Swing's repaint catches up to the changes. Playing with other applications (Windows Explorer (Native), Firefox (C++?), and Eclipse (Java)) I notice that they all have this same problem - contrary to what the folks in the link above say - but they minimize the problem by having a grey fill color, which is far less visually jarring

Building a GUI for a Sudoku Solver (Complete with ASCII Example)

一曲冷凌霜 提交于 2019-11-29 06:23:58
问题 . OVERVIEW, SAMPLE Hello everyone, I have created a basic Sudoku solver that can solve most problems fairly quickly. I still have a lot of work ahead of me to make it solve even the hardest problems, but I'd like to try to implement a basic JFrame GUI first. I have worked with internet applets in the past, but never before with JFrames. I want to create something similar to the image below (for starters): ----------------------------------------------------------------------------------------

Java rounded corners on JFrame?

家住魔仙堡 提交于 2019-11-29 04:45:23
I have a login JFrame for my application and i would like to make the corners of the frame rounded by a few pixles. Id like to do this without using transparency on the JFrame and having to use a background image inside a JPanel - is this possible? It's possible with undecorated frame, consider the following example: JFrame frame = new JFrame(); frame.setUndecorated(true); frame.setShape(new RoundRectangle2D.Double(10, 10, 100, 100, 50, 50)); frame.setSize(300, 200); frame.setVisible(true); This code works on Java 7. For Java 6 (since update 10) you can do the same with AWTUtilities