jframe

java实现UDP通信

纵然是瞬间 提交于 2019-12-06 00:06:11
1.概述 用DatagramSocket与DatagramPacket实现UDP通信. UDP比TCP相对来说简单一点,不需要等待连接,且处理数据不需要用输出输出流,只需要DatagramPacket.严格来说,UDP没有服务端和客户端之分,只有发送端与接收端之分. 2.发送端 发送端首先建立DatagramSocket与DatagramPacket,设置好DatagramPacket的端口与ip,再通过DatagramSocket发送. (1)创建DatagramSocket与DatagramPacket DatagramSocket socket = new DatagramSocket(55555); 这里的55555端口是指数据通过这个端口发送. (2)创建DatagramPacket InetAddress ip = InetAddress.getByName("127.0.0.1"); int port = 12345; String message = "message"; DatagramPacket packet = new DatagramPacket(message.getBytes(),message.getBytes().length,ip,port); 构造方法DatagramPacket()的四个参数分别为 发送的字节数组 字节数组长度 ip 端口

java swing hello world

风格不统一 提交于 2019-12-06 00:02:47
1.概述 一个简单的java swing程序hello world,只有一个button 2.源码 import javax.swing.*; public class server { public static void main(String[] args) { JFrame jFrame = new JFrame("title"); JButton button = new JButton("Test button"); jFrame.add(button);//把button添加到JFrame中 jFrame.setSize(300,300);//设置JFrame大小 jFrame.setVisible(true);//设置可见,不然的话看不到 } } 3.第一次修改 有没有觉得有点奇怪,整个button占满了窗口? 没错,少了一个JPanel: import javax.swing.*; public class server { public static void main(String[] args) { JFrame jFrame = new JFrame("title"); JPanel jPanel = new JPanel(); JButton button = new JButton("Test button"); jPanel.add(button);

java swing hello world

一个人想着一个人 提交于 2019-12-06 00:01:36
1.概述 一个简单的java swing程序hello world,只有一个button 2.源码 import javax.swing.*; public class server { public static void main(String[] args) { JFrame jFrame = new JFrame("title"); JButton button = new JButton("Test button"); jFrame.add(button);//把button添加到JFrame中 jFrame.setSize(300,300);//设置JFrame大小 jFrame.setVisible(true);//设置可见,不然的话看不到 } } 3.第一次修改 有没有觉得有点奇怪,整个button占满了窗口? 没错,少了一个JPanel: import javax.swing.*; public class server { public static void main(String[] args) { JFrame jFrame = new JFrame("title"); JPanel jPanel = new JPanel(); JButton button = new JButton("Test button"); jPanel.add(button);

How to set position of objects in JFrame?

匆匆过客 提交于 2019-12-05 22:27:32
I have Labels and JButtons i want to define the position in JFrame. import java.awt.*; import java.net.InetAddress; import java.net.UnknownHostException; import javax.swing.*; public class GuiFrame extends JFrame { public static void main(String[] args) throws UnknownHostException { JFrame f = new JFrame("This is a test"); f.setSize(400, 150); JRadioButton ButtonServer = new JRadioButton("Server"); JRadioButton ButtonClient = new JRadioButton("Client"); InetAddress thisIp = InetAddress.getLocalHost(); Label lip = new Label("Your IP is : " + thisIp.getHostAddress()); Label setup = new Label(

Does getContentPane().add() mean the same as add()

你。 提交于 2019-12-05 22:07:35
问题 Does getContentPane().add() mean the same as add() ? public class TestFrame extends JFrame{ public TestFrame() { JLabel label = new JLabel("jo"); getContentPane().add(label); add(label); } } 回答1: Does getContentPane().add() mean the same as add() ? Yes, since 1.5+. 回答2: Mostly. To make things "easier", addImpl was changed to forward to the content pane, but in uncommon corner cases it doesn't (for instance, the content pane needs to be added somehow). This method is overridden to

Java adding ImageIcon to JLabel

青春壹個敷衍的年華 提交于 2019-12-05 22:00:30
问题 I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame . It has worked in the past for me and now is not, i can't see what I did wrong. I have tried printing the current working directory and changing where I get my image to match that. It is likely that the problem is not getting the image, since my (filefinder or filereader or something like that) can find it without problems, but I cannot correctly add it (the ImageIcon ) to the JLabel , or

Java Swing JFrame Background is not showing

强颜欢笑 提交于 2019-12-05 19:32:47
I don't know why the background color is not showing on my Jframe. Below is the code that I tried. When I call AnimatedDialogBox animatedDialogBox = new AnimatedDialogBox("Saving TransSet form", dataSheetTable); Its not showing the exact color that I needed. It shows without any background color. The AnimatedDialogBox class is per below : import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing

Output jFrame to second monitor if possible

梦想与她 提交于 2019-12-05 18:41:49
i have a jFrame on Swing in Java, and i want it to output to second monitor, if this monitor exists. I tried this (by this page) to get resolutions of displays GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j < gs.length; j++) { GraphicsConfiguration[] gc = gs[j].getConfigurations(); for (int i = 0; i < gc.length; i++) { Rectangle gcBounds = gc[i].getBounds(); int xoffs = gcBounds.x; int yoffs = gcBounds.y; } } But then i watch in debugger to xoffs and yoffs for my first monitor (1360*768) xoffs = 1360

Unresizable JFrame pack error

╄→гoц情女王★ 提交于 2019-12-05 17:59:57
JFrame 's pack() method won't work every time when the window is not resizable it seems. Try it for yourself (it may need a few retries) please: import javax.swing.*; import java.awt.*; public class FramePackBug { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { for (int i = 0; i < 20; i++) { JFrame window = new JFrame("Buggy"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setResizable(false); window.setBackground(Color.RED); JPanel jp = new JPanel() { public void paintComponent(Graphics g) { g.setColor(Color.GREEN); g

How to detect if one swing window is above another one

给你一囗甜甜゛ 提交于 2019-12-05 16:33:52
I have several unrelated JFrames opened in my app. And on app exit I should remember the preferences of all of them to restore them the same view they was before exit. The problem is when these frames are located one above another, they should be restored in the same order. Another way the smaller frames would not be visible under bigger ones. QUESTION: How can I detect the deep location each of frames in this case? UPD: I detect this case by comparing the frames' location/size pairs , but I still do not how to resolve my issue. UPD2: Note: the several jframes ui is not default state. One