質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

解決済

1回答

852閲覧

うまくフレームが表示されない

yukkuri

総合スコア624

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

0グッド

0クリップ

投稿2018/10/13 03:50

前提・実現したいこと

現在、フレームを表示するプログラムで、フレーム内のベージュっぽい色のパネル(?)がずれて表示されてしまいます。そのため、背景色を変更しても背景色で埋め尽くされない状況です。

発生している問題・エラーメッセージ

えらーはありません。

該当のソースコード

java

1package org.ylib.base.swing; 2 3import java.awt.Color; 4import java.awt.Dimension; 5import java.awt.Point; 6import java.awt.Rectangle; 7import java.awt.event.ComponentEvent; 8import java.awt.event.ComponentListener; 9 10import javax.swing.JFrame; 11 12public class JylFrame extends JFrame implements ComponentListener 13{ 14 /** フレームのタイトルを示す */ 15 private String title = "frame"; 16 /** フレームの x 位置、y 位置を示す */ 17 private Point xy = new Point( 100, 100 ); 18 /** フレームの横幅、高さを示す */ 19 private Dimension widthHeight = new Dimension( 1280, 960 ); 20 /** フレームのサイズ変更可否を示す */ 21 private boolean resizable = false; 22 /** フレームの背景色を示す */ 23 private Color background = new Color( 0xff000000, true ); 24 25 public JylFrame() 26 { 27 super(); 28 initFrame(); 29 } 30 31 public JylFrame( boolean undecorated ) 32 { 33 setUndecorated( undecorated ); 34 new JylFrame(); 35 } 36 37 private void initFrame() 38 { 39 setfSize( widthHeight ); 40 setfLocation( xy ); 41 setfBackground( 0xff000000 ); 42// setfResizable( false ); 43 setDefaultCloseOperation( 3 ); // EXIT_ON_CLOSE 44 addComponentListener( this ); 45 } 46 47 public Color getfBackground() 48 { 49 return background; 50 } 51 52 public void setfBackground( int argb ) 53 { 54 setfBackground( new Color( argb, true ) ); 55 } 56 57 public void setfBackground( Color c ) 58 { 59 background = c; 60 61 getContentPane().setLocation( 0, 0 ); 62 getContentPane().setBackground( background ); 63 } 64 65 public Rectangle getBounds() 66 { 67 return new Rectangle( getfX(), getfY(), getfWidth(), getfHeight() ); 68 } 69 70 public void setfBounds( Rectangle r ) 71 { 72 xy = new Point( r.x, r.y ); 73 widthHeight = new Dimension( r.width, r.height ); 74 } 75 76 public void setfBounds( int fx, int fy, int fwidth, int fheight ) 77 { 78 xy.x = fx; xy.y = fy; 79 widthHeight.width = fwidth; 80 widthHeight.height = fheight; 81 82 setfLocation( getfX(), getfY() ); 83 setfSize( getfWidth(), getfHeight() ); 84 } 85 86 public Point getfLocation() 87 { 88 return xy; 89 } 90 91 public int getfX() 92 { 93 return xy.x; 94 } 95 96 public int getfY() 97 { 98 return xy.y; 99 } 100 101 public void setfLocation( Point p ) 102 { 103 xy = p; 104 } 105 106 public void setfLocation( int fx, int fy ) 107 { 108 xy.x = fx; xy.y = fx; 109 } 110 111 public void setfLocationOnCenter( int wWidth, int wHeight ) 112 { 113 int locationw = ( wWidth - ( widthHeight.width + 22 ) ) / 2; 114 int locationh = ( wHeight - ( widthHeight.height + 56 ) ) / 2; 115 116 setLocation( locationw, locationh ); 117 } 118 119 public boolean getfResizable() 120 { 121 return resizable; 122 } 123 124 public void setfResizable( boolean fresizable ) 125 { 126 resizable = fresizable; 127 128 setResizable( resizable ); 129 } 130 131 public int getfHeight() 132 { 133 return widthHeight.height; 134 } 135 136 public Dimension getfSize() 137 { 138 return widthHeight; 139 } 140 141 public int getfWidth() 142 { 143 return widthHeight.width; 144 } 145 146 public void setfSize( Dimension d ) 147 { 148 widthHeight = d; 149 150 getContentPane().setPreferredSize( d ); 151 pack(); 152 setfTitle( title ); 153 } 154 155 public void setfSize( int fwidth, int fheight ) 156 { 157 widthHeight.width = fwidth; 158 widthHeight.height = fheight; 159 160 setfSize( new Dimension( widthHeight.width, widthHeight.height ) ); 161 } 162 163 public String getfTitle() 164 { 165 return title; 166 } 167 168 public void setfTitle( String ftitle ) 169 { 170 title = ftitle; 171 172 String newtitle = title + " " + getfWidth() + "x" + getfHeight(); 173 setTitle( newtitle ); 174 } 175 176 @Override public void componentResized( ComponentEvent e ) 177 { 178 widthHeight.width = getWidth() - 22; 179 widthHeight.height = getHeight() - 56; 180 181 setfTitle( title ); 182 } 183 184 @Override public void componentMoved( ComponentEvent e ) 185 { 186 xy.x = getX(); xy.y = getY(); 187 } 188 189 @Override public void componentHidden( ComponentEvent e ){} 190 191 @Override public void componentShown( ComponentEvent e ){} 192} 193

java

1import org.ylib.base.swing.JylFrame; 2 3public class GameMain 4{ 5 public static void main( String[] args ) 6 { 7 JylFrame jf = new JylFrame(); 8 jf.setfResizable( true ); 9 jf.setfSize( 640, 480 ); 10 jf.setfLocationOnCenter( 1920, 1080 ); 11 jf.setVisible( true ); 12 } 13} 14

試したこと

Stackoverflowというサイトに同じような問題がありました。しかし、それとは違いスレッドは使用していません。

補足情報(FW/ツールのバージョンなど)

java8 windows10 です。
IDEは使用していません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

私の環境ではソースコードをコピペで動かしてもなんら問題はありませんでした。もし他のクラスがあるのであれば、それも全て載せておいてください。
そしてフレームどうこう以前に、質問に書かれていることからわかる限りではソースコードは無駄なものが大半を占めているようです。

Java

1import javax.swing.*; 2import java.awt.*; 3import java.awt.event.ComponentAdapter; 4import java.awt.event.ComponentEvent; 5 6public class GameMain { 7 8 private static final int DEFAULT_WIDTH = 640; 9 private static final int DEFAULT_HEIGHT = 480; 10 11 private GameMain() { 12 // フレームを作成 13 JFrame jFrame = new JFrame(); 14 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 15 jFrame.getContentPane().setBackground(Color.BLACK); 16 jFrame.setTitle("frame " + DEFAULT_WIDTH + " x " + DEFAULT_HEIGHT); 17 jFrame.setVisible(true); 18 19 // サイズ、位置変更 20 Insets insets = jFrame.getInsets(); 21 jFrame.setSize(DEFAULT_WIDTH + insets.left + insets.right, DEFAULT_HEIGHT + insets.top + insets.bottom); 22 jFrame.setLocationRelativeTo(null); 23 24 // リスナーを追加 25 jFrame.addComponentListener(new ComponentAdapter() { 26 @Override 27 public void componentResized(ComponentEvent e) { 28 int width = jFrame.getWidth() - insets.left - insets.right; 29 int height = jFrame.getHeight() - insets.top - insets.bottom; 30 31 jFrame.setTitle("frame " + width + " x " + height); 32 } 33 }); 34 } 35 36 public static void main(String[] args) { 37 new GameMain(); 38 } 39}

一応これだけで質問にあるコードと同じ動作をするのですが、そちらの環境ではこのコードは動作しますか?

投稿2018/10/13 05:55

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yukkuri

2018/10/13 06:06

動きました!しかし、どこが原因でバグが起きたのかがわからないです。もしも心当たり等ありましたら教えてくださいませんか?
退会済みユーザー

退会済みユーザー

2018/10/13 17:02

「フレーム内のベージュっぽい色のパネル(?)がずれて表示される」ということがどういう状態なのかこちらの環境で再現されず、よく分からないのでなんとも言えませんが、 ・JFrame の getBounds() をオーバーライドしていること ・getContentPane().setLocation(0, 0) の記述 の二つが怪しい気がします。 詳しい方であればはっきり原因を突き止められるかもしれませんが、私は Swing はまともに使ったことがないのでこれぐらいしか見つかりませんでした。 ですが、そもそものソースコードがぐちゃぐちゃであることが、デバッグを難しくしている一番の要因ですかね。
yukkuri

2018/10/14 02:19

そうですか。すいませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問